@Nevin, regarding the concept of one protocol that refines another "overriding" the behavior of the latter, recently, I saw this statement (which might refer to the entire concept of overriding, or might merely refer to usage of the keyword override
):
Thus far, my experimentation with the Q: P
relationship, and having the Q
protocol restate a requirement of the P
protocol has not identified any change to witness-selection (a/k/a conformance) behavior. I've experimented both with and without an override
keyword. My experiments have not been exhaustive. Have you seen a case in which merely repeating the existence of the requirement actually changes behavior?
No error is produced by usage of the override
keyword with a protocol requirement, but I'm not sure it is legal. I believe it works only because it was made legal for private usage in the Standard Library to give the compiler a hint that the restatement of a requirement ought not generate all the boilerplate that goes with a requirement. The restatement of the requirement seems to be in support of giving the compiler hints for inference of associatedtype
s. In turn, the usage of override
seems to be an assertion that the requirement is merely a restatement, which helps keep binary size down.
In this same context, @_nonoverride
was introduced. Its purpose is not so clear to me. Somewhat contradictorily, its effect seems to be to force an override. I believe it is spelled as it is, because override
is used to tell the compiler, in this context: "I know this looks like a declaration of a requirement, but it is really just a restatement of a requirement that already exists, so ignore it (except for type inference)." While @_nonoverride
is telling the compiler: "I know that this declaration of a requirement looks like it merely is a meaningless restatement, but let's pretend it is a brand new requirement, and wire this protocol's PWT on that basis." All of that is 100% supposition based on observations and a couple of pull request comments.
I will emphasize to those who might read this post, I believe override
and @_nonoverride
are private, undocumented, and should not be used in the wild, as they could be changed at any time, without warning.
Does anyone have any insight about override
/ @_nonoverride
? @Douglas_Gregor, @jrose, @Slava_Pestov, @xwu, to name some possibilities.
[Update: For one the pull request comments to which I refer, see [Standard library] Audit protocol member overrides in protocols.]