Created
December 2, 2016 18:17
-
-
Save amberstar/5c31ed26d60982533825c618b9caef29 to your computer and use it in GitHub Desktop.
Modify behavior of struct
This file contains hidden or 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
struct ModifyBehavior { | |
var value: Int | |
static var operation1: (inout ModifyBehavior) -> Int = { instance in | |
instance._operationToUse = operation2 | |
return instance.value | |
} | |
static var operation2: (inout ModifyBehavior) -> Int = { instance in | |
instance.value = 0; | |
return 0 | |
} | |
var _operationToUse: (inout ModifyBehavior) -> Int = operation1 | |
mutating func operation() -> Int { | |
return _operationToUse(&self) | |
} | |
init(value: Int) { | |
self.value = value | |
} | |
} | |
var mod = ModifyBehavior(value: 10) | |
mod.operation() // 10 | |
mod.operation() // 0 | |
mod.operation() // 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment