Spelling empty parameter packs as just type identifier

Would it be possible to enable spelling empty parameter packs as just the type identifier? We have this Callback typealias defined, and it's somewhat unfortunate to always have to spell Callback<> for the very common empty callback case.

typealias Callback<each Argument> = (repeat each Argument) -> Void
let callback: Callback // error: Reference to generic type 'Callback' requires arguments in <...>

In Swift, omission of type arguments doesn't mean a type doesn't take any arguments, it means that the compiler should infer the arguments. For example:

let a: Array = [1, 2, 3]  // a is inferred to be Array<Int>
let b: Array  // error: nothing to infer the arguments from

So your Callback example without arguments already has a meaning today in Swift:

typealias Callback<each Argument> = (repeat each Argument) -> Void
func f(_ x: Int) -> Void {}
let c1: Callback = f  // c1: (Int) -> ()

I suspect that it could lead to confusion if the meaning of overloading the type parameter list was done in this way.

There have been discussions of having default values for type parameters in the past, which could potentially be another path for what you're looking for (if there were a way to explicitly spell the empty pack in a type parameter definition), but that might still require empty <> for the same reason as above. I think that's an open question today since nothing has yet been proposed.

6 Likes

That’s really interesting! Thanks for sharing those details – maybe we could say that when omitting the type arguments without additional context, the parameter pack is inferred to be empty? That seems to be consistent with today's meaning of type argument omission.