# Make protocols conforming to protocols as only struct/enum/class types can conform to protocols now

**URL:** <https://forums.swift.org/t/make-protocols-conforming-to-protocols-as-only-struct-enum-class-types-can-conform-to-protocols-now/39696>\
**Category:** Pitches\
**Tags:** generics, protocols\
**Created:** [August 25, 2020, 10:57am UTC](https://forums.swift.org/t/make-protocols-conforming-to-protocols-as-only-struct-enum-class-types-can-conform-to-protocols-now/39696 "2020-08-25T10:57:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![absoftware](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/absoftware/32/12954_2.png) [@absoftware](https://forums.swift.org/u/absoftware)\
**Post date:** [August 25, 2020, 10:57am UTC](https://forums.swift.org/t/make-protocols-conforming-to-protocols-as-only-struct-enum-class-types-can-conform-to-protocols-now/39696/1 "2020-08-25T10:57:55Z")

</div>

Why this is so hard to implement container of protocols in Swift?

My custom container implemented as

```
let listeners = Listeners<MyListener>()

```

raises issue:

```
Value of protocol type 'MyListener' cannot conform to 'ListenerProtocol'; only struct/enum/class types can conform to protocols

```

It would be nice to make it working for protocols. Full example:

```
protocol ListenerProtocol: class {}

class Listeners<ListenerType: ListenerProtocol> {

    private class ListenerWeakReference<ListenterType: ListenerProtocol> {
        weak var listener: ListenterType?
        init(listener: ListenterType) {
            self.listener = listener
        }
    }

    private var listeners: [ListenerWeakReference<ListenerType>] = []

    func add(listener: ListenerType) {
        self.listeners.append(ListenerWeakReference<ListenerType>(listener: listener))
    }

    func remove(listener: ListenerType) {
        self.listeners.removeAll { $0.listener === listener }
    }

    func forEach(_ closure: (_ listener: ListenerType) -> Void) {
        for listenerWeakReference in self.listeners where listenerWeakReference.listener != nil {
            closure(listenerWeakReference.listener!)
        }
    }
}

protocol MyListener: ListenerProtocol {
    func didSomethingHappen()
}

let listeners = Listeners<MyListener>() // Value of protocol type 'MyListener' cannot conform to 'ListenerProtocol'; only struct/enum/class types can conform to protocols

```

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [August 25, 2020, 12:03pm UTC](https://forums.swift.org/t/make-protocols-conforming-to-protocols-as-only-struct-enum-class-types-can-conform-to-protocols-now/39696/2 "2020-08-25T12:03:19Z")

</div>

Hi! This is something that core team wants to fix, check out "Clarifying existential types" section of [Improving the UI of generics](https://forums.swift.org/t/improving-the-ui-of-generics/22814)

---

<div class="post-metadata">

**Author:** ![Nickolas\_Pohilets](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nickolas_pohilets/32/10659_2.png) [@Nickolas\_Pohilets](https://forums.swift.org/u/Nickolas_Pohilets)\
**Post date:** [August 25, 2020, 1:51pm UTC](https://forums.swift.org/t/make-protocols-conforming-to-protocols-as-only-struct-enum-class-types-can-conform-to-protocols-now/39696/3 "2020-08-25T13:51:14Z")

</div>

What you are facing here is a combination of the two known problems -

- [Existential subtyping as generic constraint](https://forums.swift.org/t/existential-subtyping-as-generic-constraint/31584)
- [Should weak be a type? - #22 by John\_McCall](https://forums.swift.org/t/should-weak-be-a-type/34032/22)

> [@Should weak be a type?](https://forums.swift.org/t/should-weak-be-a-type/34032/22):
>
> However, there is currently no generic constraint that can fully express "is any kind of class reference type";

So, in the ideal world, generic signature of your class would look like this:

```swift
class Listeners<ListenerType> where ListenerType: any ListenerProtocol, ListenerType: Referencable {
...
}

```
