Created
September 28, 2017 04:29
-
-
Save khawajafarooq/7a6015a4c789aa843a5aeffc41736f89 to your computer and use it in GitHub Desktop.
Underlying concept of value semantics in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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