Why does the "pipe" operator have a higher precedence?

// This does not compile because `->` has a higher precedence than `+`.
let foo = 3 + 4 -> Int.toString

What was the rationale behind this choice of precedence? Really curious. The low precedence pipe is a very powerful tool to simplify complex transforms.

I guess @cristianoc or @Maxim would know?

My guess is because essentially you are doing this:

let foo = 3 + Int.toString(4)

Which will not compile because the types don’t match.

I think the precedence is similar to JS:

3 + toString(4)
// or
3 + (4).toString()

Mind that pipes are syntactic sugar for function calls (example one) and are used to model method calls (example two).

Also, parens are a powerful tool to change precedence to your liking. Just sayin’ :innocent:

1 Like