Why are blankspaces allowed between Module and function?

Recently after spending an hour debugging/staring at a code, I found out that one or more blankspaces are allowed when invoking a function. For example,

iex(7)> IO. puts "hello"
hello
:ok  
iex(8)> IO.   puts("hello")
hello
:ok

I personally think this will introduce bugs. Here’s one instance where I ran into a problem when I mistakenly typed a period instead of a comma:

def handle_call(request, _caller, state) do
    {:reply, request. state}
end

When invoking a GenServer call, I got,

14:19:30.744 [error] GenServer :my_registry terminating
** (UndefinedFunctionError) function :some_request.state/0 is undefined (module :some_request is not available)
    :some_request.state()
    echo_gen_server.ex:13: EchoGenServer.handle_call/3
    (stdlib) gen_server.erl:661: :gen_server.try_handle_call/4
    (stdlib) gen_server.erl:690: :gen_server.handle_msg/6
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

What is the reasoning behind allowing blankspaces between Module and function invocation? Shouldn’t the compiler flag this and reported as error?

2 Likes

This is quite normal behaviour for a parser. With most parser technologies it is considerably more difficult to add this kind of whitespace sensitivity (and it would disallow splitting map access chains over multiple lines).

Try adding whitespace in random places in various languages, I think you’ll be surprised where it’s allowed. :slight_smile:

3 Likes

It just makes parser simpler, as it tokenise it to {{:., [], [IO, :puts]}, [], ["hello"]} (simplified). As you can see . is independent operator, lust like + or |>. Disallowing spaces around only one operator just for sake of I-have-no-idea-what would be pointless.

1 Like

This reminds me of the PHP “downto” operator --> :slight_smile::

$a = 100;
while ($a --> 0) { echo $a, PHP_EOL; }
1 Like

It came out from C, I do not know if there is any previous example of -- operator.