Impossible to type cast or test non-copyable existential types?

I've been doing (more) playing around with non-copyable types, and have noticed that as?, as! and is all are implicit consumes. For the as operators that makes sense to me; less so for is.

protocol Event: ~Copyable { }

struct MyEvent: Event { }

let event: any Event = MyEvent()

event as? MyEvent // bad
event as! MyEvent // bad
event is MyEvent // also bad

Two questions:

  1. Is there a sensible reason why is currently a consume?
  2. It feels like there should be a way to do a scoped constrain of a non-copyable existential type without consuming it, but once again: is there some reason why that doesn't make sense?
1 Like

The Swift runtime generally has not been updated to dynamically handle noncopyable types, and dynamic casting is not supported for them. Note that MyEvent and any Event as written in your example are still implicitly Copyable since you haven't specified that they are also ~Copyable.

4 Likes