Cast Any to Sendable

I'm continuing with the migration towards Swift 6. Within one of our libraries, I want to check whether a parameter object: Any? confirms to Sendable. I tried the most obvious one:

if let sendable = object as? Sendable {
}

But that results into the compiler error "Marker protocol 'Sendable' cannot be used in a conditional cast".

Is there an other way to do this?

That’s not surprising. Sendable is a marker protocol, which means it has no runtime presence, which means that a runtime type check isn’t feasible.

I’ve never seen folks bump into this before because a key goal of Swift concurrency is to have the compiler validate your concurrency at build time. Checking stuff at runtime kinda runs counter to that goal.

I don’t think there’s an obvious path forward here. I suspect you’ll have to take a step back and look at some bigger picture options, like converting this parameter to a generic or adding another routine that takes any Sendable.

If you can provide more details about the big picture, I’d be happy to offer further advice.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I have this issue, in the case where I get a userInfo dictionary from an OS API which is [String: Any], and I need to store it as a [String: any Sendable] to be able to fit it into my own code which is Sendable. What to do in this case?

Cast Any to Sendable
 
 
Q