Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift parameter packs and Sendable

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?

like image 311
Sergey A. Novitsky Avatar asked Oct 13 '25 01:10

Sergey A. Novitsky


1 Answers

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)
    }
}
like image 62
Sweeper Avatar answered Oct 16 '25 04:10

Sweeper