What prevents us from piping to arithmetic operators?

I’m aware that you can use Kernel to pipe with arithmetic operators. But why do we have to? Is it a style concern, or a compiler limitation that stops us from doing: 5 |> +(2)?

I believe it conflicts with the unary + operator, which has a higher precedence than |>.

x = quote do: 5 |> +(2)
# {:|>, [context: Elixir, import: Kernel],
# [5, {:+, [context: Elixir, import: Kernel], [2]}]}

x |> Macro.expand(__ENV__) |> Macro.to_string() |> Macro.format_string!()

# warning: piping into a unary operator is deprecated, please use the qualified name. For example, Kernel.+(5), instead of +5

# ** (ArgumentError) cannot pipe 5 into +2, the :+ operator can only take two arguments

That argument error, seems to have an error in its statement though. I suspect it was allowed at some point and became disallowed because of precedence/parsing issues.

4 Likes