Does this worth an issue?

iex(1)> quote do
...(1)>   1 -> 2
...(1)> end
[{:->, [], [[1], 2]}]
iex(2)> quote do
...(2)>   f(1 -> 2)
...(2)> end
** (SyntaxError) iex:3:7: syntax error before: '->'

I think there should be no syntax error, this is a valid syntax

iex(1)> quote do
...(1)>   (1 -> 2) |> f
...(1)> end
{:|>, [context: Elixir, import: Kernel],
 [[{:->, [], [[1], 2]}], {:f, [], Elixir}]}
iex(2)> quote do
...(2)>   (1 -> 2) |> f()
...(2)> end
{:|>, [context: Elixir, import: Kernel], [[{:->, [], [[1], 2]}], {:f, [], []}]}

Even this works

case and with are special forms and directly implemented in the compiler. You cannot use those arrows as is within functions/macros. But there are some ways to work around this, e.g. like done here within stream_data: stream_data/ex_unit_properties.ex at master · whatyouhide/stream_data · GitHub

1 Like

Thank you

case and with are special forms and directly implemented in the compiler. You cannot use those arrows as is within functions/macros

I can see that

But the question is still unanswered. I can use -> outside cases and withs
Like this:

iex(1)> defmodule X do
...(1)>   defmacro x(x), do: quote(do: unquote(inspect(x)))
...(1)> end
{:module, X,
 <<70, 79, 82, 49, 0, 0, 4, 220, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 157,
   0, 0, 0, 16, 8, 69, 108, 105, 120, 105, 114, 46, 88, 8, 95, 95, 105, 110,
   102, 111, 95, 95, 10, 97, 116, 116, 114, ...>>, {:x, 1}}
iex(2)> import X
X
iex(3)> x 123
"123"
iex(4)> (1 -> 2) |> x()
"[{:->, [line: 4], [[1], 2]}]"
iex(5)> x(1 -> 2)
** (SyntaxError) iex:5:5: syntax error before: '->'

So, is there any idea behind this behaviour? Or is it just a bug?

Hmm

iex(5)> Kernel.|>((1 -> 2), x())
"[{:->, [line: 5], [[1], 2]}]"
iex(6)> Kernel.|>((1 -> 2), x)
"[{:->, [line: 6], [[1], 2]}]"

Aha, I see

f(1 -> 2)   # Error
f((1 -> 2)) # Valid

But this is wrong

Turns out this is intentional

1 Like