[erlang-questions] The -module line

Joe Armstrong <>
Fri Nov 21 14:06:42 CET 2014


On Fri, Nov 21, 2014 at 12:21 PM, Loïc Hoguin <> wrote:
> Hi,
>
> Something has been bugging me for a very long time now.
>
> The -module line is mandatory. It also must be the same as the file name
> minus the extension .erl or compilation fails.

Not quite. Here's an example where the two differ

$ cat foo.erl
-module(bar).
-export([me/0]).

me() ->
     "bar".

$erl
1> {ok, Mod, Code} = compile:file("foo.erl",[binary]).
{ok,bar,
    <<70,79,82,49,0,0,2,0,66,69,65,77,65,116,111,109,0,0,0,
      46,0,0,0,5,3,98,...>>}

2> code:load_binary(bar,"glurk",Code).
{module,bar}

3> bar:me().
"bar"

The reasons for this are lost in the mists of time - if memory serves me well
it had to do with some deep skulduggery in distributed Erlang.

Personally I'd like to have

In a file foo.erls

  -module(bar).
  ...
  -module(foo).
  ...
  -module(baz).
  ...

Like elixir :-)

/Joe





>
> http://erlang.org/doc/reference_manual/modules.html
>
>> -module(Module).
>> Module declaration, defining the name of the module. The name Module,
>> an atom, should be the same as the file name minus the extension erl.
>> Otherwise code loading will not work as intended.
>
>> This attribute should be specified first and is the only attribute
>> which is mandatory.
>
> Instead of forcing the programmer to write down the file name, minus the
> .erl extension, and then have the compiler check that the programmer did a
> good job and can have a cookie, can't we have the compiler simply get the
> module name from the file name? The compiler is looking there *regardless*
> so maybe it can save us some time and fetch it itself?
>
> The attribute is mandatory, yes. But it can be just another attribute the
> compiler fills in automatically.
>
> Or perhaps I'm missing an obvious reason as to why we have to fill in a line
> identical to the file name we are feeding the compiler?
>
> I would like to recommend a change that will make the compiler fetch and use
> the file name automatically if the attribute is missing, and error out if
> it's different than the one in the file.
>
> --
> Loïc Hoguin
> http://ninenines.eu
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions


More information about the erlang-questions mailing list