Last active
August 29, 2015 14:23
-
-
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.
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
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