Different precedences on the left and the right? Any prior art?
This is an excerpt from c++ proposal p2011r1:
Let us draw your attention to two of the examples above:
x |> f() + y is described as being either f(x) + y or ill-formed
x + y |> f() is described as being either x + f(y) or f(x + y)
Is it not possible to have f(x) + y for the first example and f(x + y) for the second? In other words, is it possible to have different precedence on each side of |> (in this case, lower than + on the left but higher than + on the right)? We think that would just be very confusing, not to mention difficult to specify. It’s already hard to keep track of operator precedence, but this would bring in an entirely novel problem which is that in x + y |> f() + z(), this would then evaluate as f(x + y) + z() and you would have the two +s differ in their precedence to the |>? We’re not sure what the mental model for that would be.
To me, the proposed precedence seems desirable. Essentially, "|>" would bind very loosely on the LHS, lower than low-precedence operators like logical or, and it would bind very tightly on the RHS; binding directly to the function call to the right like a suffix. So, x or y |> f() * z()
would be f(x or y) * z()
. I agree that it's semantically complicated, but this follows my mental model of how I'd expect this operator to work.
Is there any prior art around this? I'm not sure where to start writing a parser that would handle something like this. Thanks!