When I inherit a protocol adding mutating keyword to the property with the same name from the parent protocol, I can't create a concrete type even there's no compile error for the protocols.
Check out this code:
protocol FooProtocol {
var value: Int { get }
}
protocol MutatingFooProtocol: FooProtocol {
var value: Int { mutating get }
}
// no compile error so far.
struct Bar: MutatingFooProtocol {
private var storage: Int
var value: Int {
mutating get {
self.storage += 1
return self.storage
}
}
}
This produces compile error that Bar is not conforming to protocol FooProtocol. but adding non mutating var value: Int does not solve the problem. It creates another compile error Invalid redeclaration of 'value'.
jrose
(Jordan Rose)
2
Not being able to create the conforming type seems correct to me. Any non-mutating getter is a valid mutating getter, but not the other way around. But you're right that there should probably be an error on the definition of MutatingFooProtocol. Mind filing a bug?
I totally agree with you that MutatingFooProtocol was not even supposed to be compiled successfully. But I'm not sure this is a bug so that should be fixed or proposed as a evolution.
jrose
(Jordan Rose)
4
I think it's okay as just a bug. It can probably be a warning to start, so that people's projects don't immediately break.
1 Like