# SF-0011 and existentials

**URL:** <https://forums.swift.org/t/sf-0011-and-existentials/90723>\
**Category:** Discussion\
**Tags:** foundation, existential, notification, anyobject\
**Created:** [November 13, 2026, 10:54am UTC](https://forums.swift.org/t/sf-0011-and-existentials/90723 "2026-11-13T10:54:04Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [November 13, 2026, 10:54am UTC](https://forums.swift.org/t/sf-0011-and-existentials/90723/1 "2026-11-13T10:54:04Z")

</div>

I've tried to migrate some of the notifications in our codebase to [SF-0011](https://github.com/swiftlang/swift-foundation/blob/main/Proposals/0011-concurrency-safe-notifications.md) and faced a challenge.

We often erase specific classes to protocols for unit testing purposes. I.e. you often get in a batch:

```swift
protocol Foo: AnyObject {}
class FooImpl: Foo {}
class FooMock: Foo {}

```

And if this entity can post notifications, then notifications are part of the contract of the protocol, so I would put `FooEventMessage: NotificationCenter.MainActorMessage` next to the the `protocol Foo` and would use `any Foo` as `Subject`.

But it does not work, because `NotificationCenter` methods have `Message.Subject: AnyObject` requirement and `(any Foo)` does not satisfy it.

The intention behind `Message.Subject: AnyObject` is valid, so IMO it is a language issue. And conformance `(any P): AnyObject` should be valid when `P` conforms to `AnyObject`. Or there should be some other protocol that covers this functionality.

As a workaround, it might be possible to remove the constraint from `NotificationCenter` methods and instead check for stable identity at the runtime. @Philippe_Hausler, @cthielen, WDYT?

---

<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:** [November 13, 2026, 4:50pm UTC](https://forums.swift.org/t/sf-0011-and-existentials/90723/2 "2026-11-13T16:50:28Z")

</div>

I believe Jordan's reply here answers the question as to the language issue:

> [@Error: 'MyProtocol' is not convertible to 'AnyObject'](https://forums.swift.org/t/error-myprotocol-is-not-convertible-to-anyobject/9817/8):
>
> The implementation reason for why this doesn't work is because Swift protocol values have to keep track of two things: the value being referenced, and what we call the "witness table", which describes how the dynamic type conforms to the protocol. (You can think of this like a struct of functions, one for each requirement in the protocol, though it's a little more complicated than that in practice.) That means it can't be treated as a single reference like a concrete class instance can. Objecti…

Basically, as I understand it, the 'meaning' of conforming to `AnyObject` is that under the hood you're a single reference. But any (ha) existential `any P`, even if `protocol P: AnyObject`, is not laid out that way so doesn't actually fulfill the layout constraint required by conformance to `AnyObject`.

It would have to be explicitly opt-in on an ABI-stable platform to change that, non-backward deployable, and in any case carry runtime performance penalties.

---

<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:** [November 13, 2026, 6:06pm UTC](https://forums.swift.org/t/sf-0011-and-existentials/90723/3 "2026-11-13T18:06:41Z")

</div>

Perhaps you could make `Subject` a generic parameter:

```swift
struct FooEventMessage<Subject: Foo>: NotificationCenter.MainActorMessage

```

This builds fine but I didn't test it thoroughly.

---

<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:** [November 13, 2026, 6:16pm UTC](https://forums.swift.org/t/sf-0011-and-existentials/90723/4 "2026-11-13T18:16:19Z")

</div>

Maybe we could extend meaning of `AnyObject` conformance to " **first** word is a reference, and the rest are bitwise copiable bits". From the first glance, it does sound reasonably backwards-compatible. But maybe there are some challenges with co-/contra-variance.

> [@tera](#):
>
> Perhaps you could make `Subject` a generic parameter:
> 
> ```swift
> struct FooEventMessage<Subject: Foo>: NotificationCenter.MainActorMessage
> 
> ```
> 
> This builds fine but I didn't test it thoroughly.

I was thinking about it. It does not work for use cases that want to observe messages regardless of the posting instance. If ObjC bridging is fully implemented, then `FooEventMessage<A>` can be converted to `Notification` and then parsed as `FooEventMessage<B>`. But this bridging is optional. And for cases without any ObjC usages, I would prefer to skip it.

---

<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:** [November 13, 2026, 6:31pm UTC](https://forums.swift.org/t/sf-0011-and-existentials/90723/5 "2026-11-13T18:31:16Z")

</div>

> [@Nickolas\_Pohilets](#):
>
> I was thinking about it. It does not work for use cases that want to observe messages regardless of the posting instance.

I see. Another option - mock (in this case) with a class instead of a protocol and make the `Subject` of that common base class.
