Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.