# 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:** 59

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [June 23, 2020, 12:53am UTC](https://forums.swift.org/t/need-help-understanding-protocols-and-generics/37564/59 "2020-06-23T00:53:42Z")

</div>

> [@mattrips](#):
>
> 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?

`override` is implied by having the same requirement stated in a more-refined protocol as in a protocol it inherits from. It is allowed to be stated explicitly primarily to suppress the warning produced by `-warn-implicit-overrides`.

What you surmise is correct. When a protocol requirement "overrides" one from an inherited protocol, this requirement always gets the same witness as that of the inherited protocol. At the implementation level, there is no new entry for an `override` requirement in the witness table.

> [@mattrips](#):
>
> 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.

This is correct.

> [@mattrips](#):
>
> In this same context, `@_nonoverride` was introduced. Its purpose is not so clear to me.

`@_nonoverride` says that the restated requirement does _not_ have to have the same witness in a more-refined protocol as in the protocol it inherits that originally stated the requirement. In the implementation, this means that a `@_nonoverride` requirement introduces a new entry in the witness table. If you look at where `@_nonoverride` is used in the Standard Library, they are for cases where the semantics or complexity of operations (like `distance(from:to:)`) change in the protocol inheritance hierarchy: `distance(from:to:)` on a `Collection` is forward-only, on a `BidirectionalCollection` can go backwards, and on a `RandomAccessCollection` is

`@_nonoverride` is very, very important when dealing with conditional conformances, because it gives you more-capable witnesses in your conformances to protocols like `BidirectionalCollection`. Without it, for example, a wrapper around a `Collection` that had a conditional conformance to `BidirectionalCollection` would not be able to provide a more-specialized implementation of the wrapper semantics for bidirectional collections.

Note how this is the same problem we've [been discussing](https://forums.swift.org/t/an-implementation-model-for-rational-protocol-conformance-behavior/37171/138), where we really want to be able to pick a more-specialized witness when we have a more capable type. The `override` / `@_nonoverride` dance was an implementation band-aid to get conditional conformances in the standard library to provide (mostly) the right behavior, using an ABI we could live with.

Doug

---

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