What's the syntax to create a parameter packed generic type with zero type parameters?

In the implemented proposal they say something to the effect of “type parameter pack stores a list of zero or more type parameters” in a few different places. What's the syntax to create a generic type or an instance of a generic type with zero type parameters? There was a followup proposal that was also implemented in 5.9 that includes this snippet

struct V<each T> {} 
V<>.self

But this code doesn’t compile in 5.9. It complains that <> isn’t a valid operator.

A few minutes after posting I found this bug report that had a solution. Referring to a type with an empty generic parameter pack causes "Cannot find operator '<>' in scope" error. · Issue #68290 · apple/swift · GitHub

You can get the above to compile by putting a space in between the two angle braces.

struct V<each T> {} 
V< >.self
4 Likes

Ah glad you found that.

It's definitely a better workaround than what I got :smiley:

struct V<each T> {}

func workaround<each T>(_: repeat each T) -> V<repeat each T>.Type {
	return V<repeat each T>.self
}

print(workaround()) // => V<Pack{}>
1 Like

Might be backwards-incompatible to change. :-/ Someone could already have a postfix operator <>. Maybe in Swift 6 mode that can require parentheses?

2 Likes

Ahhh that makes sense. I don't know what policy is of editing proposals after they've been accepted, but I've opened a PR to the proposal to fix the example in question: Update parameter pack syntax in proposal to match implemented syntax by twof · Pull Request #2191 · apple/swift-evolution · GitHub

I also ran into a related compiler crash and filed a report here: Methods on parameter packed generic types specialized with zero types cannot be used without crashing the compiler · Issue #69313 · apple/swift · GitHub

Thanks for the help!

1 Like

postfix operator <> isn’t even a good defense, as currently it does not correctly select that interpretation in cases of ambiguity: SwiftFiddle - Swift Online Playground