What is the syntax for expanding operators from parameter packs?

Static methods allow for this syntax:

protocol Protocol {
  static func method(_: Self)
}

func method<each P: Protocol>() -> (repeat (each P) -> Void) {
  (repeat (each P).method)
}

I can't figure out how to do it with operators, other than using the expanded metatypes in a function.

func equals<each Element: Equatable>() -> (repeat (each Element, each Element) -> Bool) {
  (repeat { _ in (==) } ((each Element).self))
}

Does this work for you?

func equals<each Element: Equatable>() -> (repeat (each Element, each Element) -> Bool) {
    func getEq<T: Equatable>(of type: T.Type) -> (T, T) -> Bool {
        (==)
    }
    
    return (repeat getEq(of: (each Element).self))
}

No. That's the same thing that I posted, but adding a name.

It seems to satisfy the compiler in a Playground, but I get an IRGen error when I try to run

(repeat (==) as (each Element, each Element) -> Bool)

should work, as

let equal = (==) as (Bool, Bool) -> Bool

does, but it's currently incorrectly parsed. I would file a GitHub issue.

1 Like