SR-142 mutating function in protocol extension erroneously requires var declaration of class variables
Issue Description:
The `mutating` keyword on protocols is enforced by the compiler on Object types when the type is given by a generic constraint. The `mutating` keyword is not otherwise enforced on objects.
This is inconsistent, I think it should work in this case as well.
Error:
Cannot use mutating member on immutable value: 'base' is a 'let' constant.
Fix-it Change 'let' to 'var' to make it mutable
Example
protocolSomeProtocol {
mutatingfuncupdate()
}
structObjectWrapper<T: AnyObject> {
letbase: T
}
Swift 4's implementation of subclass existentials fixed a lot of problems here.
Note that the behavior is correct though – a mutating member on a class-constrained type still has to be invoked on an lvalue, because it might be implemented in a protocol extension method that re-assigns self.
swift-ci commentedJul 30, 2016
Additional Detail from JIRA
md5: 68a8c8b12a6c73d972f7bfeeba12af89
relates to:
var
declaration of class variablesIssue Description:
The `mutating` keyword on protocols is enforced by the compiler on Object types when the type is given by a generic constraint. The `mutating` keyword is not otherwise enforced on objects.
This is inconsistent, I think it should work in this case as well.
Error:
Example
This works:
This does not work:
This does not work:
The text was updated successfully, but these errors were encountered: