If a type obtains a method or computed property by conforming to a protocol, is that method or property a member of the type? I'm not sure of the semantic intent behind the concept.
For example:
protocol P { var id: String { get } }
extension P { var id: String { "P" } }
class C: P {}
class D: C {
override var id: String { "D" } // ERROR - if override keyword is removed, no error
}
By conforming to P
, C
gains the ability to access an id
property and a default implementation for it.
I understand that, when I access id
directly on C
, that access does not go through the protocol conformance (i.e., it does not check the witness for the id
requirement). Instead, it statically dispatches directly to id
. Semantically, that feels like id
is a member of C
.
OTOH, when I declare D
as a subclass of C
, and then override the id
property of C
, I trigger an error: "Property does not override any property from its superclass." The compiler is telling me that C
does not have a property named id
. If I remove the override
keyword, the code compiles, and the usual error that one would get when overriding without using the override
keyword is not generated ("Overriding declaration requires an 'override' keyword"). Semantically, that feels like id
is NOT a member of C
.
So, which is it? Is id
a member of C
or not?
If id
is not a member of C
, then what are the semantics of accessing id
on C
?
In this regard, consider the following statement made in the Language Reference refrained the nature of an extension declaration:
If the type name [of an extension] is a protocol type, the extension extends all types that conform to that protocol.
Thus, the extension on P
should be regarded as extending C
, thus including the id
declaration as a member of C
.