[erlang-questions] Why we need a -module() attribute?

Judson Lester nyarly@REDACTED
Wed Feb 24 18:44:07 CET 2016


On Tue, Feb 23, 2016 at 3:50 PM Richard A. O'Keefe <ok@REDACTED>
wrote:

>
>
> On 24/02/16 8:53 am, Kenneth Lakin wrote:
>  > It doesn't seem to be the case. This works just fine for me with
> Erlang 18:
> > $ ls
> > $ mkdir -p dir/with/parents
> > $ echo "-module (test).
> >> -compile (export_all).
> >>
> >> run() -> ok." > dir/with/parents/test.erl
> > $ erlc dir/with/parents/test.erl
> > $ ls
> > dir  test.beam
> > $
> There is an important difference.  The OP wanted the equivalent of
>    -module(dir_with_parents_test).  Try it and you get
> m% erlc dir/with/parents/test.erl
> /home/cshome/o/ok/erlang.d/test.beam: Module name
> 'dir_with_parents_test' does not match file name 'test'
>

Indeed. Looking at the docs for erlc and compile, I can see that there's a
couple of things that influence this, maybe. The key mystery, for me, is
how compile:file/3 determines its product's path. Maybe it uses the File
argument, but that isn't 100% clear.

The error can be suppressed with an option, but it appears that would wreck
code loading - maybe moving the file afterwards? There'd be problems with
concurrent builds, though, and it doesn't do much for  avoiding an
intermediate file.

There's an option to set the source name, but again, not clear if that's
just used by module_info/1 or to construct the output path.

Finally, I suppose you could use the binary option to produce the module in
memory and then write that to a file - would that work, though?

I guess the point of frustration I come down to is that the file name and
the module name don't have to be the same ... except that they do.

One concrete use case: I've recently split up a gigantic module into
several smaller ones, but they all need to share a series of record
definitions. I'd love to be able to collect them all into a directory and
put the record defs in a .hrl there, and be reasonably sure that the record
is only available to those modules.

Judson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20160224/1bc6d285/attachment.htm>


More information about the erlang-questions mailing list