# Protocols with associated protocols?

**URL:** <https://forums.swift.org/t/protocols-with-associated-protocols/17022>\
**Category:** Discussion\
**Tags:** protocols\
**Created:** [October 15, 2018, 8:08pm UTC](https://forums.swift.org/t/protocols-with-associated-protocols/17022 "2018-10-15T20:08:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmousseau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jmousseau/32/44102_2.png) [@jmousseau](https://forums.swift.org/u/jmousseau)\
**Post date:** [October 15, 2018, 8:08pm UTC](https://forums.swift.org/t/protocols-with-associated-protocols/17022/1 "2018-10-15T20:08:19Z")

</div>

Would it be possible to have a protocol with associated protocols? For instance:

```swift
protocol Store {

    associatedprotocol RecordCodable

    func save(item: RecordCodable)

}

protocol SyncEngine: Store {

    associatedtype LocalStore: Store
    associatedtype RemoteStore: Store

    typealias RecordCodable = LocalStore.RecordCodable & RemoteStore.RecordCodable

    var localStore: LocalStore { get }
    var remoteStore: RemoteStore { get }

}

extension SyncEngine {

    func save(item: RecordCodable) {
        localStore.save(item: item)
        remoteStore.save(item: item)
    }

}

```

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [October 15, 2018, 9:37pm UTC](https://forums.swift.org/t/protocols-with-associated-protocols/17022/2 "2018-10-15T21:37:54Z")

</div>

There was a thread about this a while ago:

> [@\[Pitch\] Generalized supertype constraints](https://forums.swift.org/t/pitch-generalized-supertype-constraints/7121):
>
> One of the most frequent frustrations I encounter when writing generic code in Swift is the requirement that supertype constraints be concrete. When I mentioned this on Twitter ([https://twitter.com/anandabits/status/929958479598534656\](https://twitter.com/anandabits/status/929958479598534656%5C)) Doug Gregor mentioned that this feature is smaller and mostly straightforward to design and implement ([https://twitter.com/dgregor79/status/929975472779288576\](https://twitter.com/dgregor79/status/929975472779288576%5C)). I currently have a PR open to add the high-level description of this feature found below to the gen…

I'd love to see this happen.

---

<div class="post-metadata">

**Author:** ![anthonylatsis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anthonylatsis/32/39232_2.png) [@anthonylatsis](https://forums.swift.org/u/anthonylatsis)\
**Post date:** [October 15, 2018, 11:15pm UTC](https://forums.swift.org/t/protocols-with-associated-protocols/17022/3 "2018-10-15T23:15:00Z")

</div>

Generalized supertypes are great, but as the author alludes, the example provided still wouldn't be type-safe without some mechanism that would allow you to define an associated type or generic parameter that must be a protocol type (or of some other nature), i.e.

```swift
protocol P {
  protocol associatedtype Foo
}

func foo<protocol T>(_ arg: T) { ... }

```

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [October 16, 2018, 2:51pm UTC](https://forums.swift.org/t/protocols-with-associated-protocols/17022/4 "2018-10-16T14:51:44Z")

</div>

This request is a little bit different than generalized supertype constraints as it introduces the ability to abstract over protocols. Generalized supertype constraints _do not_ let you abstract over anything new. I would really love to see them happen and hope @Douglas_Gregor finds a way to sneak them into Swift 5 🤞. I have bumped into this limitation in the generic system very often.

A generalized supertype constraint is just a new way of constraining an associated type or generic parameter. This constraint requires an argument to be a subtype of the type bound that is bound to the constraining type variable (associated type or other generic parameter).

The `associatedprotocol` pitch is a subset of something more general: an `associatedconstraint` which would need to be paired with `constraintalias`. This is an _extremely_ powerful feature I would _really_ love to see someday. It is related to the [Constraint Kinds](https://downloads.haskell.org/~ghc/7.4.1/docs/html/users_guide/constraint-kind.html) extension in Haskell.

I have thought about starting a pitch thread but it seems wildly out of scope at the moment so I haven't yet. @Joe_Groff would a discussion on this topic be useful right now?

---

<div class="post-metadata">

**Author:** ![Stan\_Smida](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stan_smida/32/10999_2.png) [@Stan\_Smida](https://forums.swift.org/u/Stan_Smida)\
**Post date:** [October 2, 2023, 6:51pm UTC](https://forums.swift.org/t/protocols-with-associated-protocols/17022/5 "2023-10-02T18:51:58Z")

</div>

Any update on this?

I'm thinking about how to abstract over graphs with different kinds of nodes. Not even sure if thinking right about it but could this once work with associated protocols?

```swift
// `SelfProtocol`? Since `Self` in this context refers to the adopter type...
protocol Node {    
    var parent: (any SelfProtocol)? { get }
    var children: [any SelfProtocol] { get }
}

protocol FieldNode: Node {}

final class TextField: FieldNode {
    var parent: (any FieldNode)?
    var children: [any FieldNode]
}

final class PickerField: FieldNode {
    var parent: (any FieldNode)?
    var children: [any FieldNode]
}

protocol SourceNode: Node {}

final class File: SourceNode {
    var parent: (any SourceNode)?
    var children: [any SourceNode]
}

final class Link: SourceNode {
    var parent: (any SourceNode)?
    var children: [any SourceNode]
}

```

This would allow to build and work with any graphs of "`Node`" shape but separated "Field" and "Source" kinds. Without abstract classes, or other workarounds or boilerplates.
