Skip to content

Instantly share code, notes, and snippets.

@amberstar
Created December 2, 2016 18:17
Show Gist options
  • Save amberstar/5c31ed26d60982533825c618b9caef29 to your computer and use it in GitHub Desktop.
Save amberstar/5c31ed26d60982533825c618b9caef29 to your computer and use it in GitHub Desktop.
Modify behavior of struct
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