# Need Help Understanding Protocols and Generics

**URL:** <https://forums.swift.org/t/need-help-understanding-protocols-and-generics/37564>\
**Category:** Using Swift\
**Created:** [June 14, 2020, 12:55pm UTC](https://forums.swift.org/t/need-help-understanding-protocols-and-generics/37564 "2020-06-14T12:55:56Z")\
**Posts on this page:** 1\
**Showing post:** 42

<div class="post-metadata">

**Author:** ![mattrips](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattrips/32/4079_2.png) [@mattrips](https://forums.swift.org/u/mattrips)\
**Post date:** [June 19, 2020, 11:53pm UTC](https://forums.swift.org/t/need-help-understanding-protocols-and-generics/37564/42 "2020-06-19T23:53:12Z")

</div>

> [@Nevin](#):
>
> The protocol requirement `Q.id` is the same as the protocol requirement `P.id` . The former is an override of the latter, since it is not marked `@_nonoverride` .

@Nevin, regarding the concept of one protocol that refines another "overriding" the behavior of the latter, recently, I saw this [statement](https://forums.swift.org/t/an-implementation-model-for-rational-protocol-conformance-behavior/37171/108) (which might refer to the entire concept of overriding, or might merely refer to usage of the keyword `override`):

> [@An Implementation Model for Rational Protocol Conformance Behavior](https://forums.swift.org/t/an-implementation-model-for-rational-protocol-conformance-behavior/37171/108):
>
> Wow, when did `override` become legal on protocols?

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](https://github.com/apple/swift/commit/f8e53d912954e63f967fc6e0450731a40f4cc08b#diff-959f54de5f6b0806f2b23ae938bf1452).]

---

_[View the full topic](https://forums.swift.org/t/need-help-understanding-protocols-and-generics/37564)._
