# Is it possible to expand which protocols can act as types?

**URL:** <https://forums.swift.org/t/is-it-possible-to-expand-which-protocols-can-act-as-types/38878>\
**Category:** Discussion\
**Tags:** protocols\
**Created:** [July 29, 2020, 9:44pm UTC](https://forums.swift.org/t/is-it-possible-to-expand-which-protocols-can-act-as-types/38878 "2020-07-29T21:44:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [July 29, 2020, 9:44pm UTC](https://forums.swift.org/t/is-it-possible-to-expand-which-protocols-can-act-as-types/38878/1 "2020-07-29T21:44:33Z")

</div>

If we have a protocol without any associated types nor any `Self` requirements, then we can declare a variable of that protocol to act as a multi-type box/pointer for any conforming type. Having either at least one associated type or one `Self`-marked member disqualifies conforming types from using such boxes.

But, if there was a way to specify a protocol and 100% of its associated types, and the protocol does _not_ have `Self` requirements, shouldn't we be able to create protocol boxes the same way?

For instance, here's a try at a Swift-y regex:

```
public protocol PatternMatcher {
    associatedtype Element

    func matchingPrefix<C: Collection>(for collection: C) -> C.Index?
        where C.Element == Element
}

```

Obviously, I can't create a `PatternMatcher` existential to hold any conforming type, especially if the `Element` can change between assignments. But I should be able to make `PatternMatcher<where Element == Character>` boxes. Otherwise, I currently have no alternative besides ripping out `Element` and locking it to `Character`, preventing the generalization of regex matching to any element type.

Here's some older threads that were suggested to me, that I feel may actually be applicable:

- [Is it possible to use protocols with associated types as opaque return types?](https://forums.swift.org/t/is-it-possible-to-use-protocols-with-associated-types-as-opaque-return-types/36605)
- [[Idea] Typealias for protocols with associated types](https://forums.swift.org/t/idea-typealias-for-protocols-with-associated-types/1317)

---

<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:** [July 30, 2020, 4:36am UTC](https://forums.swift.org/t/is-it-possible-to-expand-which-protocols-can-act-as-types/38878/2 "2020-07-30T04:36:04Z")

</div>

This was just asked four days ago:

> [@Constrained associated type on protocol](https://forums.swift.org/t/constrained-associated-type-on-protocol/38770):
>
> Why associated type requirement persists when a constrain is defined on a protocol? For instance: protocol P1 { var id: String { get } } var p1Array: [P1] // OK protocol P2: Identifiable where Self.ID == String { } var p2Array: [P2] // Protocol 'P2' can only be used as a generic constraint because it has Self or associated type requirements Thank you

---

<div class="post-metadata">

**Author:** ![CTMacUser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ctmacuser/32/1802_2.png) [@CTMacUser](https://forums.swift.org/u/CTMacUser)\
**Post date:** [July 31, 2020, 1:10am UTC](https://forums.swift.org/t/is-it-possible-to-expand-which-protocols-can-act-as-types/38878/3 "2020-07-31T01:10:39Z")

</div>

What @kozlow described in that thread (and @jarod did in the older thread) are in the same category as what I want. But I don't want to introduce a new protocol and corresponding name; I want the facility available directly.

That older thread eventually referenced a not-quite-as-old thread by @Joe_Groff about adding a broader form of the idea. However, it drifted towards the existential box types for protocols with `Self` or associated type constraints to have those members that express those constraints be suppressed. (Those members can be activated indirectly through Stupid Generic Tricks.) But that is the exact opposite of what I want; I want the protocol's full API available.

Let's call protocols with `Self` or associated type constraints "PAT"s. Right now, only non-PAT protocols can have existential instances of them declared and used as variables. My desire is that what I call "fully specified PATs" can also be used as a type of an existential instance. It would be the name of the protocol followed by a `where` clause of constraints. But the `where` clause is protected by the angle brackets ("\<\>") used for conventional generics. The boundaries for which PAT-restrained protocols can be turned into fully specified PATs are:

- If the protocol has `Self` requirements, the only ones allowed are read-only properties of type `Self` and methods with a return type of `Self`. Using `Self` in a function parameter type or in an indirect way in a return type prevents the protocol from being used as a fully specified PAT.
- Every associated type that does not have a default must be specified in the `where` clause. (You are allowed to specify associated types that do have a default if you want to override them.) It can be either a type equality or a base-class and/or protocol constraint. Equality specifications always work towards building a fully specified PAT, but not all class/protocol constraints are compatible; subsequent rules will mention when class/protocol constraints cannot be used.
- (A protocol used in a class/protocol constraint can be either a non-PAT or fully specified PAT.)
- An associated type that is left as a default is OK only if either it doesn't depend on the explicit types (directly or indirectly) or if the finalized type is the same for a given set of explicit types. This usually means that all explicit types that touch the defaulted types have to use equality constraints and not class/protocol ones. If a default associated type depends on an also-defaulted sibling associated type, then that dependency has to be resolved first.
- An associated type that uses a class/protocol constraint (instead of an equality constraint) can only appear in members the same way `Self` can (_i.e._ read-only property or direct method return).

Hopefully this narrower use case will be easier to implement instead of full-blown generalized existentials or somewhat-generalized existentials but the PAT parts uselessly suppressed. One of the older posts did mention a [pull request](https://github.com/apple/swift/pull/21576) that could be used, but the author wants to do it over, and I don't know if s/he did.
