Skip to content

Instantly share code, notes, and snippets.

@sozorogami
Last active August 29, 2015 14:23
Show Gist options
  • Save sozorogami/377892b02d8d5f804b53 to your computer and use it in GitHub Desktop.
Save sozorogami/377892b02d8d5f804b53 to your computer and use it in GitHub Desktop.
When mutating methods from a protocol are called on a variable conforming to that protocol, the compiler misses the mutation and emits a warning. Casting the variable to a type that implements the protocol will silence the warning.
protocol Increaser {
var value: Int { get set }
mutating func increase()
}
func doIncrease(var myIncreaser: Increaser) { // Parameter 'myIncreaser' was never mutated; consider
myIncreaser.increase() // changing to 'let' constant
myIncreaser.value
}
struct Foo: Increaser {
var value: Int
init() {
value = 0
}
mutating func increase() {
value++
}
}
func increaseAFoo(var myFoo: Foo) { // No warning emitted.
myFoo.increase()
myFoo.value
}
doIncrease(Foo())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment