# Pre-Pitch: Explicit protocol fulfilment with the 'conformance' keyword

**URL:** <https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246>\
**Category:** Pitches\
**Created:** [September 12, 2022, 4:53pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246 "2022-09-12T16:53:11Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 12, 2022, 4:53pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/1 "2022-09-12T16:53:11Z")

</div>

Hello,

I'd like to test an idea: the introduction of a new keyword in front of declarations: `conformance`.

* * *

**EDIT** - the pitch is now located at: [Explicit Protocol Fulfillment](https://gist.github.com/groue/202977d33e929c834673ceb17b2426c9).

The original post is below:

* * *

The `conformance` keyword means: "this declaration fulfills a protocol requirement". Its presence is never required. However, when present, the program is ill-formed unless the declaration does match a protocol requirement.

For example:

```swift
protocol P {
    func foo()
}

struct S1: P {
    func foo() { ... } // OK
}

struct S3: P {
    conformance func foo() { ... } // OK
}

struct S4: P {
    // error: function fooo() does not fulfill any protocol requirement.
    conformance func fooo() { ... }
}

```

* * *

### Motivation

Unlike class overrides that must be prefixed with the `override` keyword, declarations that fulfill protocol conformances do not need any specific decoration. This creates a few problems for programmers.

The first problem appears when the rules of type inference miss the protocol requirement.

For example, this code compiles, but can miss the programmer's intent:

```swift
protocol Measurable {
    var length: Double { get }
}

extension Measurable {
    var length: Double { 0 }
}

struct Meter: Measurable {
    // Misses protocol requirement because 'Int' is not 'Double'
    let length = 1
}

// Prints 0.0 instead of 1.0
func test(_ p: some Measurable) { print(p.x) }
test(Meter()) 

```

The second problem appears when protocols evolve in time, and change their requirements. For example, this can happen with libraries when they bump their major version and introduce breaking changes. When the user upgrades the library, the compiler won't provide any help for updating methods and removing the ones that have become useless:

```diff
 // MyLib
 protocol Program {
- func crash()
- func run()
+ func run(arguments: [String])
 }
 extension Program {
- func run() { }
+ func run(arguments: [String]) { }
 }

```

```swift
import MyLib

struct HelloWorld: Program {
    // Dead code
    func crash() { fatalError("Bye") }
    
    // Needs to be updated
    func run() { print("Hello World!") }
}

```

Note that in both examples above, the actual intent of the programmer can not be determined from looking at the code alone. It is impossible to tell if `length` and `run()` are intended to fulfill protocol requirements, or just happen to fulfill protocol requirements. This explains why it is impossible to address those problems in the current state of the language.

Yet both problems do exist, for programmers whose intent is to fulfill protocol requirements.

### Proposed Solution

We propose that the intent to fulfill a protocol requirement can be made visible in the program, with the `conformance` keyword.

The `conformance` keyword would create a compiler error in our two examples:

```swift
struct Meter: Measurable {
    // error: property 'length' does not fulfill any protocol requirement.
    // note: protocol Measurable requires property 'length' with type 'Double'
    // note: candidate has non-matching type 'Int'
    conformance let length = 1
}

struct HelloWorld: Program {
    // error: function 'crash()' does not fulfill any protocol requirement.
    conformance func crash() { fatalError("Bye") }
    
    // error: function 'run()' does not fulfill any protocol requirement.
    conformance func run() { print("Hello World!") }
}

```

Those compiler errors help the user apply the proper fixes (solve type inference ambiguities, remove obsolete functions, and update the modified requirements):

```swift
struct Meter: Measurable {
    conformance let length: Double = 1
}

struct HelloWorld: Program {
    conformance func run(arguments: [String]) { print("Hello World!") }
}

```

### Detailed Design

The `conformance` keyword has the compiler search for a matching declaration in all statically known conformances at the location it is used. If no such declaration exists, the compiler emits an error.

Here are a few examples:

```swift
protocol P1 { func foo() }

// =====

struct S1: P1 {
    // OK: matches P1.foo()
    conformance func foo()
}

// =====

struct S2 {
    // OK: matches P1.foo()
    conformance func foo()
}
extension S2: P1 { }

// =====

struct S3 { }
extension S3: P1 { }
extension S3 {
    // OK: matches P1.foo()
    conformance func foo()
}

// =====

protocol P2: P1 { }
struct S4: P2 {
    // OK: matches P1.foo()
    conformance func foo()
}

// =====

protocol P3: P1 { }
extension P3 {
    // OK: matches P1.foo()
    conformance func foo() { ... }
}

// =====

protocol P4 { }
extension P4 {
    // error: function 'foo()' does not fulfill any protocol requirement.
    conformance func foo() { ... }
}

```

### Known Limits

There are situations where the `conformance` keyword won't be able to fully help the programmer.

- When a type conforms to two protocols that declare the same requirement, the `conformance` keyword does not tell which conformance, or both, are intended.

- In some valid programs, the method that fulfills the conformance is not declared at a location where the compiler has enough static information to accept the `conformance` keyword. In the code below, we can't use the 'conformance' keyword here, because when `Q.foo` is compiled, the `P` protocol is not statically known to be conformed to.

Despite those limitations, the `conformance` keyword brings more safety to the language, and more programmer confidence in the correctness of the program.

### Future Directions

1. When a type conforms to several protocols that declare the same requirement, the `conformance` keyword could be extended with the name of the protocol:

2. The `conformance` keyword could pave the way for a public version of [`@_implements(ProtocolName, Requirement)`](https://github.com/apple/swift/blob/main/docs/ReferenceGuides/UnderscoredAttributes.md#_implementsprotocolname-requirement):

3. The `conformance` keyword could be made to accept statically invisible requirements, by using an explicit protocol name:

4. The `conformance` keyword could be made able to "auto-fix" some near misses:

### Alternatives Considered

- `@conformance` instead of `conformance`.

- Requiring `conformance`, juste like we require `override`.

- Requiring the name of the fulfilled protocol.

---

<div class="post-metadata">

**Author:** ![pyrtsa](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pyrtsa/32/740_2.png) [@pyrtsa](https://forums.swift.org/u/pyrtsa)\
**Post date:** [September 12, 2022, 7:17pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/2 "2022-09-12T19:17:47Z")

</div>

That would be a welcome change! +1

Here's another motivating example and a common gotcha – `conformance` should also catch it when trying to override what's only an extension method.

```swift
protocol P {
    func foo()
    //func bar() // <- Uncommenting this would fix the error.
}

extension P {
    func bar() { ... }
}

struct S: P {
    conformance func foo() { ... } // OK

    // error: function bar() does not fulfill any protocol requirement.
    conformance func bar() { ... }
}
```

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [September 12, 2022, 7:40pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/3 "2022-09-12T19:40:30Z")

</div>

> [@gwendal.roue](#):
>
> The first problem appears when the rules of type inference miss the protocol requirement.
> 
> […]
> 
> The second problem appears when protocols evolve in time, and change their requirements. For example, this can happen with libraries when they

In both of these cases, the desired near-miss diagnostics already trigger when the protocol conformance is declared in its own extension (or, should—I haven’t tested the exact example, and if they don’t we should file a bug to improve the existing diagnostics).

In my view, these near-miss diagnostics are one of the really nice benefits of implementing conformances in dedicated extensions. One way of looking at it is that Swift _already implements_ the feature you’re pitching, except that it’s spelled by nesting the member inside an `extension` instead of prefixing with a keyword such as `conformance`.

There is one key shortcoming and one key advantage of the current design as compared to your pitch that I can think of:

The shortcoming is that stored properties cannot be declared as protocol-conforming implementations in an extension; this is a limitation that I and many others have long wished to be lifted for several other reasons (my very niche own pet reason is that it will allow anything expressible as `fileprivate` to be spelled without using that access level). I hope and expect that same-file extensions will be able to declare stored properties before long and would want that to come to the language regardless of what other near-miss features are added.

The advantage is that `extension` allows the user to declare not just that a member conforms a requirement of _some_ protocol, but rather _which_ protocol (or, even, protocols). This is, as you note, a shortcoming of your pitched design which has already been solved by the existing design.

(Another advantage, which I don’t think needs to be dwelled on except parenthetically, is that the existing design doesn’t require any additional keyword or other syntax—it’s simply making what users already do work better by producing helpful diagnostics when user intention is recognized.)

---

<div class="post-metadata">

**Author:** ![David\_Catmull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_catmull/32/1007_2.png) [@David\_Catmull](https://forums.swift.org/u/David_Catmull)\
**Post date:** [September 12, 2022, 9:02pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/4 "2022-09-12T21:02:18Z")

</div>

> [@xwu](#):
>
> The advantage is that `extension` allows the user to declare not just that a member conforms a requirement of _some_ protocol, but rather _which_ protocol (or, even, protocols).

Maybe this is tangential, but it seems a little weird to me that `extension A: SomeProtocol { ··· }` does two things that are actually more or less orthogonal: adding an extension, and adding a protocol conformance, which may or may not be satisfied by things in that particular extension.

Right now you can do silly stuff like this:

```swift
struct Thing { ··· }

extension Thing: ProtocolA {
  // things that satisfy ProtocolB
}

extension Thing: ProtocolB {
  // things that satisfy ProtocolA
}

```

...which I think can conceivably happen to code that grows over time and gets reorganized without enough attention to detail.

It makes me wonder if there would be value in being able to say "all members needed to satisfy protocol X are exclusively in this extension (and maybe also in the main declaration)". (I'm not suggesting all extensions should be like this; frequently it's useful to declare conformance using elements that a type already has)

---

<div class="post-metadata">

**Author:** ![wes1](https://avatars.discourse-cdn.com/v4/letter/w/848f3c/32.png) [@wes1](https://forums.swift.org/u/wes1)\
**Post date:** [September 13, 2022, 6:46am UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/5 "2022-09-13T06:46:06Z")

</div>

Perhaps permit the declaration name to be qualified with the protocol type?:

```swift
protocol P {
    func foo()
}

struct S: P {
    func P.foo() { }
}

```

Aside from error messages, the protocol name could also support refactoring, completions, and navigation.

This doesn't help when the same declaration complies with multiple protocols, but perhaps that's an anti-pattern.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [September 13, 2022, 8:46am UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/6 "2022-09-13T08:46:59Z")

</div>

+1 for the pitch. As a bike shed name also consider "conforming":

```
conforming func foo() {...}

```

> [@gwendal.roue](#):
>
> In some valid programs, the method that fulfills the conformance is not declared at a location where the compiler has enough static information to accept the `conformance` keyword. In the code below, we can't use the 'conformance' keyword here, because when `Q.foo` is compiled, the `P` protocol is not statically known to be conformed to.
> 
> ```swift
> protocol P { func foo() }
> protocol Q { }
> extension Q { func foo() { } } // <-
> struct T: P, Q { }
> 
> ```

I wonder how we handle this situation with `override` and could we do the same with `conformance`?

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [September 13, 2022, 9:47am UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/7 "2022-09-13T09:47:13Z")

</div>

> [@tera](#):
>
> I wonder how we handle this situation with `override`

I don't think you can have this problem. While you don't know which class or classes in the chain have a function with the same signature, you do know the one being overridden is the nearest ancestor which has such a function, of which there can only be one.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 13, 2022, 10:11am UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/8 "2022-09-13T10:11:37Z")

</div>

Thanks @xwu. The pitch does not rely on any code structure, such as fulfillments grouped in dedicated extensions, for several reasons:

Indeed it is not always possible to declare a fulfillment in an extension. A stored property that fulfills a protocol requirement, for example, can not be declared in an extension. But even if this would becomes possible eventually, I wouldn't rely on extensions anyway.

First, even if extension grouping could help with near-misses (it does not), it would not help with the second described problem, due to protocol evolution. I'm talking about functions that becomes useless because one requirement of a protocol has been deleted, and functions that need to update their signature. Should we start to require that `extension T: P { ... }` can **only** contain P fulfillments? Even people who group protocol conformances into a dedicated extension happen to declare more related methods inside it - and I'm not sure this would be considered "bad style".

Next, the pitch does not favor one particular programming style. Grouping fulfillments in extensions is frequent, in some social groups, but not universal:

- Some code crunching tools (I think of [jazzy](https://github.com/realm/jazzy)) don't always produce good results when conformances are declared in an extension. I've seen myself declaring all conformances upfront, at the location of the type declaration, and provide fulfillments elsewhere. It's not a personal preference, it's a pragmatic decision based on my constraints at the moment I was writing this code:

- The language does not mandate any programming style. The language comes first, style comes second, and can vary. Grouping fulfillments in an extension is not required by the language. I do not know of another language feature that requires a particular style to be enabled.

- Not all users have the same skills, or time, to polish their code. Requiring a particular style (grouping fulfillments in an extension) for enabling language features could be seen as elitist, or ableist, or pedantic for no good reason.

- If the compiler would help solving the problems described in the pitch by emitting warnings or errors, based on extension grouping, then a simple move of code (copy and paste) would create or resolve this diagnostic. I'm afraid this would bring more confusion than good: how can the same identical function be correct here, and incorrect there? Make you your mind, compiler!

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 13, 2022, 10:15am UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/9 "2022-09-13T10:15:07Z")

</div>

Thank you @wes1. Yes, a similar idea is explored in the future directions of the pitch:

```swift
struct S: P {
    conformance(P) func foo() { }
}

```

Your idea is good, but I still prefer `conformance`:

- With `conformance`, you do not have to always repeat the protocol name. This could be tedious if you have to write a dozen such methods, in order to fulfill the requirement of a protocol with a very long name.
- With `conformance`, we can suggest in the future directions a path to a public version of `@_implements(ProtocolName, Requirement)`. Your proposed syntax can not be extended in the same way.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 13, 2022, 10:19am UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/10 "2022-09-13T10:19:10Z")

</div>

> [@tera](#):
>
> As a bike shed name also consider "conforming":
> 
> ```swift
> conforming func foo() {...}
> 
> ```

English is not my mother tongue, and I always read `override` as a _name_ when in front of `func` or `var` (you never know what the brain of a stranger will do!). Hence `conformance`.

If native speakers see `override` as a verb, then `conform` would be better. If an adjective, `conforming` will certainly do. I'll let the English-native crowd choose, here :-)

---

<div class="post-metadata">

**Author:** ![tevelee](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tevelee/32/9775_2.png) [@tevelee](https://forums.swift.org/u/tevelee)\
**Post date:** [September 13, 2022, 11:58am UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/11 "2022-09-13T11:58:34Z")

</div>

+1 for the pitch

As a future direction, maybe we can satisfy protocol requirements by annotating dynamicMemberLookup subscripts with the `conformance` keyword.

```swift
protocol Requirement {
    var someProperty: Double { get }
    func someMethod(argument: Int) -> Bool
}

@dynamicMemberLookup
class Implementation: Requirement {
    conformance subscript(dynamicMember dynamicMember: String) -> Any { /* return something accordingly */ }
    conformance subscript(dynamicMember dynamicMember: String) -> Callable { Callable() }
}

@dynamicCallable
class Callable {
    func dynamicallyCall(withKeywordArguments args: KeyValuePairs<String, Any>) -> Any { /* return something accordingly */ }
}

```

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 13, 2022, 12:31pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/12 "2022-09-13T12:31:13Z")

</div>

Pitch was edited with a new "The `conformance` keyword could be made able to "auto-fix" some near misses" paragraph in the future directions.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 13, 2022, 12:32pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/13 "2022-09-13T12:32:34Z")

</div>

Maybe the pitch would be better if I made explicit an underlying idea regarding documentation.

The `conformance` keyword is pitched as optional. One does not have to use it. One has benefits using it, because it reveals some present or future mismatches between the programmer intent, and what the compiler sees. By "present mismatch", I mean typos and near-misses. By "future mismatch", I mean code that must change because a dependency has changed the definition of a protocol.

It is a tool that I initially intended for three groups of people:

1. Programmers who are aware of the benefits of the keyword, for their own code base.
2. Documentation writers, who would start recommending the use of the `conformance` keyword in the documentation of some protocols, when relevant.
3. Clueless documentation readers, who use the `conformance` keyword because that's what the sample code says in the documentation. And that's as good for them as it is good for the programmers who are aware of what they are doing.

The benefits of `conformance` for the first group have been explained above.

The benefits of `conformance` for the documentation writers is two-fold:

1. Some documentation can be simplified. The example below comes from GRDB ([original](https://github.com/groue/GRDB.swift#columns-selected-by-a-request)), and was considered necessary due to the high risk of miss due to type-inference:

2. Migration guides can be simplified. Again, consider this GRDB example ([original](https://github.com/groue/GRDB.swift/blob/master/Documentation/GRDB6MigrationGuide.md#record-changes)):

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 13, 2022, 12:55pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/14 "2022-09-13T12:55:12Z")

</div>

What about this reunion of our ideas:

The initial pitch was: the `conformance` keyword looks for statically-known protocol requirements at the location it is used (and errors out if the declaration has no match).

When used inside an extension that declares a protocol conformance, we could have the `conformance` keyword perform requirement lookup _in those protocols only_.

To illustrate, consider the following program. It compiles without error with the pitch as written. But it would not with the suggested amendment:

```swift
protocol Foo { func foo() }
protocol Bar { func bar() }

struct S { }

extension S: Bar { }
extension S: Foo {
    conformance func foo() { }
    // Initial pitch: no error
    // Amended pitch: function bar() does not fulfill any 'Foo' requirement.
    conformance func bar() { }
}

```

I'm still not 100% convinced, because I do not like the idea that moving a declaration can make it valid or invalid. Yet this change would help solving some ambiguities that are discussed in the "Known Limits" paragraph of the pitch. The balance is difficult.

Following the same idea, we could have:

```swift
extension Foo where Self: Bar {
    // Conformance lookup performed in Foo and Bar only
    conformance func bar() { ... }
}

extension Foo {
    // Conformance lookup performed in Foo and Bar only
    conformance func bar() where Self: Bar { ... }
}

```

This amendment, if implemented later, would be a breaking change (since some code would stop compiling). This means it must come with the initial proposal, or not at all (making the implementation more difficult). I humbly let the community decide if it's worth it.

---

<div class="post-metadata">

**Author:** ![filip-sakel](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/filip-sakel/32/8697_2.png) [@filip-sakel](https://forums.swift.org/u/filip-sakel)\
**Post date:** [September 13, 2022, 1:38pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/15 "2022-09-13T13:38:19Z")

</div>

Can `conformance` be added in protocol extensions for default implementations? Also, I think it’d be nice if we could have `conformance` extensions, which I think would handle the design pattern outlined by @xwu. Overall, this feature has a lot of promise. I’m interested to see if there are benefits to compilation times using this approach. Additionally, adoption is another important question. I think `conformance` should become the default as it aids readability, diagnosing near misses and potentially the compiler. If we decide that this keyword should be required, then features such as autocomplete and migration strategies should be discussed to facilitate adoption.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [September 13, 2022, 1:55pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/16 "2022-09-13T13:55:05Z")

</div>

> [@David\_Catmull](#):
>
> It makes me wonder if there would be value in being able to say "all members needed to satisfy protocol X are exclusively in this extension (and maybe also in the main declaration)". (I'm not suggesting all extensions should be like this; frequently it's useful to declare conformance using elements that a type already has)

Maybe

```swift
conformance extension MyType: SomeProtocol {
  func ...
  var ...
}

```

…could be sugar for

```swift
extension MyType: SomeProtocol {
  conformance func ...
  conformance var ...
}

```

… and that such a "conformance extension" would produce diagnostics if it included (non-private) members that did not conform to the protocol(s) listed?

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 13, 2022, 2:20pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/17 "2022-09-13T14:20:35Z")

</div>

This looks nice at first sight (maybe as a future direction, since this does not look _required_ for the pitch to provide value).

Yet beware: such a `conformance extension` would ONLY accept declarations that fulfill requirements. This is the hard version of the "group protocol conformances in extensions" styling, because even related methods would become forbidden in such an extension:

```swift
conformance extension SortedArray: Collection {
    var first: ... // OK
    var last: ... // OK
    var median: ... // error: property 'median' does not fulfill any protocol requirement. 
}

```

To me, this is the exact point were something nice that _always help_ turns into an ill-designed tool that _frequently annoys_. I let the community consider if they prefer something good or something _too_ perfect.

As you say, @sveinhal, we'd ignore methods that do not match the visibility of the protocol:

```swift
conformance extension SortedArray: Collection {
    var first: ... // OK
    var last: ... // OK
    private func someHelper() { ... } // OK
}

```

---

<div class="post-metadata">

**Author:** ![ebg](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ebg/32/23208_2.png) [@ebg](https://forums.swift.org/u/ebg)\
**Post date:** [September 13, 2022, 3:17pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/18 "2022-09-13T15:17:20Z")

</div>

This is a great pitch, and given the precedent of `override`, seems like maybe it was an oversight in the original Swift implementation. I like that it is opt-in for compatibility, but would also like to see a way to opt-in to require this.

I do think we should move directly to including the conforming protocol too, as it solves a lot of remaining problems and can leverage the implementation for `@_implements`.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [September 13, 2022, 3:23pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/19 "2022-09-13T15:23:33Z")

</div>

> [@gwendal.roue](#):
>
> This is the hard version of the "group protocol conformances in extensions" styling, because even related methods would become forbidden in such an extension:

Maybe besides the point for the pitch at hand, but for whatever it's worth: I actually prefer this hard line. Allow for private implementation details such as helper functions, but everything that is not part of the implementation, or privately aids the implementation, should be kept outside the extension, imho.

---

<div class="post-metadata">

**Author:** ![David\_Catmull](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_catmull/32/1007_2.png) [@David\_Catmull](https://forums.swift.org/u/David_Catmull)\
**Post date:** [September 13, 2022, 3:24pm UTC](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246/20 "2022-09-13T15:24:17Z")

</div>

> [@gwendal.roue](#):
>
> Yet beware: such a `conformance extension` would ONLY accept declarations that fulfill requirements. This is the hard version of the "group protocol conformances in extensions" styling, because even related methods would become forbidden in such an extension:

This is true. It would be tricky to distinguish between helper methods and methods that are actually intended to conform to another protocol.

I have had cases where I have two protocols that are very closely related, and I ended up moving methods from one protocol to the other (kinda like table view data source vs delegate - the line can be a bit blurry). With such a change, any protocol-specific extensions are now incorrectly organized, but the compiler doesn't care. So I'm looking for a way to express my intent to the compiler.

[Next page](https://forums.swift.org/t/pre-pitch-explicit-protocol-fulfilment-with-the-conformance-keyword/60246.md?page=2)
