Value parameter pack expansion in array literal

I'm playing around with parameter packs, and from SE-0393 we learn that value pack expansion is supported:

in any position that naturally accepts a list of expressions, including comma-separated lists and top-level expressions. This includes the following:

  • The elements of an array literal, e.g. [repeat each value]

Using:

  • Xcode Version 15.0 beta 3 (15A5195k)
  • swift-driver version: 1.85 Apple Swift version 5.9 (swiftlang-5.9.0.120.7 clang-1500.0.34.3)

I am having trouble reproducing this behaviour.

For instance:

func f<each V: StringProtocol>(_ v: repeat each V) -> [any StringProtocol] {
    [repeat each v] // Value pack expansion can only appear inside a function argument list or tuple element
}

Or:

extension AnyHashable {
    init<each H: Hashable>(x: Int? = nil, _ hashable: repeat each H) {
        self.init([repeat each hashable]) // Value pack expansion can only appear inside a function argument list or tuple element
    }
}

Furthermore, I am surprised that overloading doesn't work without the x: Int? = nil hack, given that:

Generic functions can be overloaded by the "pack-ness" of their type parameters. For example, a function can have two overloads where one accepts a scalar type parameter and the other accepts a type parameter pack
Currently, if a function call successfully type checks with two different overloads, the subtype is preferred. This overload ranking rule generalizes to overloads with parameter packs, which effectively means that scalar overloads are preferred over parameter pack overloads when the scalar requirements meet the requirements of the parameter pack

Is the implementation still a work in progress or has SE-0393 not been implemented to spec?

It’s just a bug in the implementation. Please file a bug report at Issues · apple/swift · GitHub.

1 Like

Thank you:

2 Likes