Hello!
I have a protocol in which there is a mutable variable. When I conform the protocol to AnyObject, then I can write a function that changes this variable without "mutating" keyword.
protocol Foo: AnyObject {
var value: Int { get set }
}
extension Foo {
func change() {
value = 1
}
}
However, when I make an extension with conditional conformance to AnyObject, code is not compile anymore.
protocol Foo {
var value: Int { get set }
}
extension Foo where Self: AnyObject {
func change() {
value = 1 // Error: Cannot assign to property: 'self' is immutable
}
}
If "mutating" is added to the function declaration, then it compiles. But this is a class, and inside the function "change" internal state is changing, not self.
What am I doing wrong?