# Using class-constrained protocol as AnyObject

**URL:** <https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988>\
**Category:** Using Swift\
**Created:** [June 26, 2018, 3:18am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988 "2018-06-26T03:18:34Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpangburn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mpangburn/32/45144_2.png) [@mpangburn](https://forums.swift.org/u/mpangburn)\
**Post date:** [June 26, 2018, 3:18am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/1 "2018-06-26T03:18:34Z")

</div>

I find myself having trouble making code generic when using class-constrained protocols in contexts requiring AnyObject. Since a protocol used as a type does not conform to the protocols from which it inherits, the code snippets below do not compile.

A couple quick examples:

If we want to box an arbitrary value to hold a reference weakly, the boxed type must conform to AnyObject, but this prevents us from boxing instances class-constrained protocols:

```swift
struct WeakBox<T: AnyObject> {
    weak var value: T?
}

protocol ClassConstrained: AnyObject { }
let x: WeakBox<ClassConstrained> // nope!

```

We encounter a similar issue with a simple wrapper around a protocol-based observation subscription model:

> **Details**
>
> ```swift
> protocol Observable {
> associatedtype Observer: AnyObject
> var observers: [ObjectIdentifier: Observer] { get set }
> }
> 
> extension Observable {
> mutating func addObserver(_ observer: Observer) {
> let id = ObjectIdentifier(observer)
> observers[id] = observer // (we'd toss some weak-boxing in here too)
> }
> }
> 
> protocol MyClassObserver: AnyObject { 
> func myClassDidSomething(_ instance: MyClass)
> }
> 
> class MyClass: Observable {
> typealias Observer = MyClassObserver // nope!
> var observers: [ObjectIdentifier: MyClassObserver]
> }
> 
> ```

Is there a straightforward way to be able to use a class-constrained protocol as a type in contexts where a class is expected? We know any instance conforming to such a protocol will be a class, so it doesn't seem unreasonable—but I can't come with up anything nice that both works in a generic context and retains type safety.

It's possible to hide dynamic casting by limiting the API surface, but this still doesn't seem ideal:

```swift
struct WeakBox<T> {
    private weak var _value: AnyObject?

    var value: T? {
        return _value as? T
    }

    init(_ value: T) {
        self._value = value as AnyObject
    }
}

```

Any suggestions?

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [June 26, 2018, 3:58am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/2 "2018-06-26T03:58:55Z")

</div>

`AnyObject` is a protocol to which all classes implicitly conform.

Not sure if this is what you were looking for, if `MyObserver` is your class you could simply have it as

```
class MyObserver {} //It would implicitly conform to AnyObject

class ObservedClass: Observable {
    typealias Observer = MyObserver
    var observers = [ObjectIdentifier: Observer]()
}

```

---

<div class="post-metadata">

**Author:** ![mpangburn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mpangburn/32/45144_2.png) [@mpangburn](https://forums.swift.org/u/mpangburn)\
**Post date:** [June 26, 2018, 4:03am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/3 "2018-06-26T04:03:25Z")

</div>

Sorry, perhaps I didn't provide enough detail. The idea is that `MyObserver` is some protocol which functions something like a delegate would, but the observed object holds a dictionary of observers to be able to notify more than just one. Something like this:

```swift
protocol MyClassObserver: AnyObject {
    func myClassDidSomething(_ instance: MyClass)
}

class MyClass: Observable {
   typealias Observer = MyClassObserver
   var observers: [ObjectIdentifier: MyClassObserver]
}

```

I've updated the OP with this example for clarity.

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [June 26, 2018, 4:14am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/4 "2018-06-26T04:14:39Z")

</div>

I could be wrong, but I think you don't need an associated type for this scenario.

```
protocol MyClassObserver: AnyObject {
    func myClassDidSomething(_ instance: MyClass)
}

protocol Observable {
    var observers: [ObjectIdentifier: AnyObject] { get set }
}

extension Observable {
    mutating func addObserver(_ observer: AnyObject) {
        let id = ObjectIdentifier(observer)
        observers[id] = observer // (we'd toss some weak-boxing in here too)
    }
}

class MyClass : Observable {
    var observers: [ObjectIdentifier : AnyObject] = [ObjectIdentifier : MyClassObserver]()
}

```

---

<div class="post-metadata">

**Author:** ![mpangburn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mpangburn/32/45144_2.png) [@mpangburn](https://forums.swift.org/u/mpangburn)\
**Post date:** [June 26, 2018, 4:21am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/5 "2018-06-26T04:21:24Z")

</div>

This works, but at the cost of some type safety. If I want to call `observer.myClassDidSomething` for each observer in `MyClass`, I must dynamically cast the values in the dictionary to `MyClassObserver` instances.

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [June 26, 2018, 4:26am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/6 "2018-06-26T04:26:35Z")

</div>

Then you could just make that as a requirement in Observable.

The change is Observable protocol requires observers to be of the type `[ObjectIdentifier: MyClassObserver]`

```
protocol MyClassObserver: AnyObject {
    func myClassDidSomething(_ instance: MyClass)
}

protocol Observable {
    var observers: [ObjectIdentifier: MyClassObserver] { get set }
}

extension Observable {
    mutating func addObserver(_ observer: MyClassObserver) {
        let id = ObjectIdentifier(observer)
        observers[id] = observer // (we'd toss some weak-boxing in here too)
    }
}

class MyClass : Observable {
    var observers = [ObjectIdentifier : MyClassObserver]()
}

```

---

<div class="post-metadata">

**Author:** ![mpangburn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mpangburn/32/45144_2.png) [@mpangburn](https://forums.swift.org/u/mpangburn)\
**Post date:** [June 26, 2018, 4:32am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/7 "2018-06-26T04:32:18Z")

</div>

Then we lose flexibility since the model is no longer generic. The approach doesn't scale; `Observable` wouldn't work for `MyOtherClass` with `MyOtherClassObserver`.

Your two suggestions summarize the challenge here: how do we retain both type safety _and_ extensibility?

---

<div class="post-metadata">

**Author:** ![mpangburn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mpangburn/32/45144_2.png) [@mpangburn](https://forums.swift.org/u/mpangburn)\
**Post date:** [June 26, 2018, 4:36am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/8 "2018-06-26T04:36:57Z")

</div>

To take the focus off the observation API, here's another example of the underlying challenge.

If we want to box an arbitrary value to hold a reference weakly, the boxed type must conform to `AnyObject`, but this prevents us from boxing instances class-constrained protocols.

```swift
struct WeakBox<T: AnyObject> {
    weak var value: T?
}

protocol ClassConstrained: AnyObject { }
let x: WeakBox<ClassConstrained> // nope!

```

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [June 26, 2018, 4:49am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/9 "2018-06-26T04:49:43Z")

</div>

This is a fair point, not sure why the compiler couldn't infer anything conforming to `ClassConstrained` would also conform to `AnyObject`

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [June 26, 2018, 1:56pm UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/10 "2018-06-26T13:56:39Z")

</div>

This is because class constrained protocols are not yet fully implemented.

---

<div class="post-metadata">

**Author:** ![somu](https://avatars.discourse-cdn.com/v4/letter/s/ce73a5/32.png) [@somu](https://forums.swift.org/u/somu)\
**Post date:** [June 26, 2018, 2:14pm UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/11 "2018-06-26T14:14:17Z")

</div>

Thanks a lot @Nobody1707 for the clarification was a bit confused

---

<div class="post-metadata">

**Author:** ![DekVester](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dekvester/32/4917_2.png) [@DekVester](https://forums.swift.org/u/DekVester)\
**Post date:** [October 8, 2018, 9:09pm UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/12 "2018-10-08T21:09:34Z")

</div>

The problem is, Swift doesn't allow you to create an instance of a generic with a protocol instead of a concrete type, if this generic's type-parameter is also constrained with a protocol. For example, you can not do this:

```
protocol Base {
}

protocol Derived: Base {
}

struct MyStruct<T: Base> {
}

var myStruct = MyStruct<Derived>()//ERROR: Using 'Derived' as a concrete type conforming to protocol 'Base' is not supported

```

This is because it opens up the doors for you to declare a static func on Base and then try to call it from inside of MyStruct type:

```
protocol Base {
    static func baseFunc()
}

struct MyStruct<T: Base> {
    func myFunc() {
        T.baseFunc()
    }
}

```

Now, the code above is okay when you just use it with a concrete type:

```
struct Concrete: Base {
}

var myStruct = MyStruct<Concrete>
myStruct.myFunc()

```

But when you use it with a protocol it is not okay:

```
var myStruct = MyStruct<Derived>
myStruct.myFunc()//not okay

```

, because Swift doesn't know how to call function .baseFunc on protocol Derived - because it does not have any particular Metatype to do so.

PS The workaround for this I'm aware of is to declare your protocols as @objc. It will unblock you to run this, but only unless you use static members in the protocols - this won't work in any case

---

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [October 8, 2018, 10:12pm UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/13 "2018-10-08T22:12:35Z")

</div>

> [@Nobody1707](#):
>
> This is because class constrained protocols are not yet fully implemented.

I'm not sure what you mean. They certainly are implemented. Protocols do not conform to other protocols though, but that is a separate issue.

---

<div class="post-metadata">

**Author:** ![Nobody1707](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nobody1707/32/2169_2.png) [@Nobody1707](https://forums.swift.org/u/Nobody1707)\
**Post date:** [October 8, 2018, 11:25pm UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/14 "2018-10-08T23:25:18Z")

</div>

Sorry, I don't know what I was thinking there.

---

<div class="post-metadata">

**Author:** ![pronebird](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pronebird/32/21721_2.png) [@pronebird](https://forums.swift.org/u/pronebird)\
**Post date:** [October 24, 2022, 10:14am UTC](https://forums.swift.org/t/using-class-constrained-protocol-as-anyobject/13988/15 "2022-10-24T10:14:45Z")

</div>

Even though `any` keyword was introduced lately, it's still impossible to create a collection of `WeakBox<SomeProtocol>` where `SomeProtocol: AnyObject`.
