Sendable enums with @Sendable case constructors

I couldn't find any information on this in the structured concurrency proposal, but has there been any discussion of making the cases of a Sendable enum act as a @Sendable function? Currently the following produces a warning:

enum Foo: Sendable {
  case bar(Int)
}

let f: @Sendable (Int) -> Foo = Foo.bar

…saying that Foo.bar is not @Sendable. As far as I can tell it seems @Sendable to me.

Is this theoretically possible at some point, or was it specifically disallowed for a particular reason?

9 Likes

I've run into something similar to this before, but in that situation it was a func rather than an enum case. This solution worked for me then; does it work here, too?

let f: @Sendable (Int) -> Foo = { .bar($0) }

Regardless, I think it is odd that this doesn't work.

Yeah that's right and I should have mentioned. Using { Foo.bar($0) } does get around the problem. However, it can make call sites of APIs look really bad, so I was hoping Foo.bar would work.

1 Like

Just came across this, and we have many many warnings across our project. Is there anything that can be done about it other than changing all the code?

1 Like

It seems that it's just a matter of making the enum Sendable, right? so probably using the long syntax just misses the warning, and it actually doesn't fix anything.

The issue presented in this thread was fixed by SE-0418.

This code will compile diagnostic-free with Swift 6 mode. For 5 mode, you'll have to enable InferSendableFromCaptures explicitly.

1 Like