The following code produces a warning about a non-sendable type:
func pack<each U: Sendable>(arg: repeat each U, function: @escaping @Sendable (repeat each U) -> Void) {
Task { @MainActor in
function(repeat each arg)
}
}
Warning:
Capture of 'arg' with non-sendable type 'repeat each U' in a @Sendable
closure
The types 'U' were marked as Sendable, but the compiler does not seem to see this. Is this a compiler bug, or I am doing something wrong?
This might be an oversight instead of a bug. The documentation for Sendable
doesn't say that value packs are Sendable
at all, so technically this is correct according to the documentation. It is likely that they just didn't consider this when designing the two features.
As a workaround, tuples are Sendable
, so if you wrap the arg
parameter in a tuple, it compiles without warnings.
func pack<each U: Sendable>(arg: repeat each U, function: @escaping @Sendable (repeat each U) -> Void) {
let tuple = (repeat each arg)
Task { @MainActor in
function(repeat each tuple)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With