Skip to content

Instantly share code, notes, and snippets.

@khawajafarooq
Created September 28, 2017 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khawajafarooq/7a6015a4c789aa843a5aeffc41736f89 to your computer and use it in GitHub Desktop.
Save khawajafarooq/7a6015a4c789aa843a5aeffc41736f89 to your computer and use it in GitHub Desktop.
Underlying concept of value semantics in swift
enum Direction {
case up
case down
case left
case right
}
struct Position {
var x: Int
var y: Int
let heading: Direction?
mutating func move(in direction: Direction) {
switch direction {
case .up: y -= 1
case .down: y += 1
case .left: x -= 1
case .right: x += 1
}
}
}
/// uncomment to check compiler error
var p1 = Position(x: 2, y: 1, heading: .left)
p1.move(in: .up)
//p1.heading = .down
let p2 = Position(x: 0, y: 0, heading: .down)
//p2.move(in: .left)
//p2.heading = .up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment