Elixir: Compiler error by space between function name and (

Created on 12 Feb 2017  路  15Comments  路  Source: elixir-lang/elixir

Environment

Fedora 25
Erlang/OTP 19 [erts-8.2.1] [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]
Interactive Elixir (1.3.1)

Current behavior

In iex, note the space between function name (f) and (:

iex(8)> defmodule A do def f (x), do: x end
** (CompileError) iex:8: can use only variables and \\ as arguments in function heads

Or as a a program (test.ex) with the same line of code:

$ elixir test.ex 
** (CompileError) test.ex:1: can use only variables and \\ as arguments in function heads

Also on more lines there is an error:

defmodule A do
  def f (x), do: x
end

Expected behavior

Either no error or a correct error message

Elixir Bug Intermediate

Most helpful comment

only variables and \\ are allowed as arguments in definition header.

If you did not intend to define a header, make sure your function definition has
the proper syntax by wrapping the arguments in parentheses and ensuring there
is no space between the function name and arguments, got:

    #{Macro.to_string({:def, [], [{name, _, args}]})}

All 15 comments

We will attempt to clarify the error message. Your example is being parsed as def f((x), do: x).

@josevalim any idea of how the error message could be clarified for this situation? Having hard times to find a good message for it. :rofl:

All I can think of is something like:

only variables and \\ are allowed as arguments in definition header.
Tip: the syntax sugar for space instead of parens can be a tricky mud pit here.
As you can see in:

    def foo (x), do: x

that is actually parsed like:

    def foo((x), do: x)

Maybe change the can be a tricky mud pit by can generate some confusion, IDK. :rofl:

only variables and \\ are allowed as arguments in definition header.

If you did not intend to define a header, make sure your function definition has
the proper syntax by wrapping the arguments in parentheses and ensuring there
is no space between the function name and arguments, got:

    #{Macro.to_string({:def, [], [{name, _, args}]})}

Just an observation: The space is accepted in the multi-line version

defmodule A do
  def f (x) do
    x
  end
end

@t-ce not if you have multiple arguments, which then is a parser error. It is generally good advice to not add spaces between the function name and arguments. :)

@t-ce yes, but this does not:

```elixir
defmodule A do
def f (x, y) do
{x, y}
end
end

Opening a PR really soon. Wait for it!

Thank you.
I tested your code @kelvinst.

defmodule A do
  def f (x, y) do
    {x, y}
  end
end

And I got

** (SyntaxError) iex:2: unexpected parentheses. If you are making a function call, do not insert spaces between the function name and the opening parentheses. Syntax error before: '('

That is, there is already a message with the space. Beside _function call_ one could add _or a function definition_

@t-ce the problem is that in def f (x), do: x the compiler understands it like a function header without the body, which cannot have any match expression too. That's why this message you got exist as it is in the first place.

@josevalim got one problem with Macro.to_string\1. I changed the message and in the tests I got def(f(x) do x end) instead of def(f((x), do: x)). I have tested here, and got the following result:

iex(1)> IO.puts Macro.to_string(quote do: def f (x), do: x)
def(f(x) do
  x
end)

Is this a problem with Macro.to_string? Or did I lose something?

Forget about it! I was just confused by the way it prints. Actually def(f(x) do x end) is an invalid function header too, the correct would be def(f(x)) do x end

I am wondering though if showing that Macro.to_string result will end up

being more confusing?

Jos茅 Valimwww.plataformatec.com.br
http://www.plataformatec.com.br/Founder and Director of R&D

Well, it's a good point. I got confused pretty easily, maybe only the long description would be less confusing and clear enough. WDYT?

Sounds good!

Jos茅 Valimwww.plataformatec.com.br
http://www.plataformatec.com.br/Founder and Director of R&D

Was this page helpful?
0 / 5 - 0 ratings