Variadic generics discussion

Yes I do. It's been two years and two Swift versions since there was any real discussion on the topic at least as far as I could tell from a forum search. I think a lot has changed since then and we've learned quite a bit that might feed into new opinions. I'd also love to see if people feel this is something that is within scope for Swift 5. If not, then discussion can still happen knowing that variadic generics will need to be pushed to Swift 6 or beyond.

There is one other discussion on variadic generics that happened last year, but it looks like it mostly devolved to a discussion on whether or not tuples should exist:

I'm interested in this because I'm working on a command line tools framework that would benefit. Currently, if I want a command line tool to be able to take parameters and have those parameters be type-safe, I have to restrict the tool's parameters to a single type.

Command<U: LosslessStringConvertible>(
    triggers: [String],
    help: String,
    numParams: NumberOfParams = .any,
    options: [ProtoOption]=[],
    onExecution: @escaping ([U], State) throws -> ()
)

In the current state someone couldn't accept a command like newperson Alex 10 passing in parameters for name and age, unless they wanted to also cast 10 to a String and deal with it at runtime.

However, with variadic generics, the type could look something more like (Using C++ syntax)

Command<...U: LosslessStringConvertible>(
    triggers: [String],
    help: String,
    numParams: NumberOfParams = .any,
    options: [ProtoOption]=[],
    onExecution: @escaping (State, U...) throws -> ()
)

The feature required here is the ability to take a variadic generic type signature, and unpack it for use as the parameter type of an instance, static, or anonymous function.

I'm interested to see what other use cases people can think of :grinning:

1 Like