[erlang-questions] Why we need a -module() attribute?
Fri Feb 19 17:04:03 CET 2016
A bit of history ..
Erlang has always had autoloading.
Initially no modules are loaded.
When a module is called that is not loaded the emulator calls the
error_handler:undefined_function(Mod, Func, Args)
The job of this function is to find the code for the module `Mod` load
the code then call apply(Mod, Func, Args)
error_handler.erl calls code.erl to actually find the code.
Code has a list of paths which it searches for a file called
<Mod>.beam It *assumes* this contains compiled code for Mod.erl.
If this is not the case weird things happen.
At the time very little thought was put into code.erl - it was quickly
hacked together. I made a generic mechanism so that users could
replace error_handler.erl with their own custom error_handler on a
per-process basis - but virtually nobody ever did this it and it was
not well documented.
code.erl and error_handler.erl are a bit of a mess (in fact the entire
code loading is a bit of a mess) - but it works and rewriting it has
never been high priority.
It's not really apparent, but parts of the system have had thousands of
hours of work done on them (binaries, bit syntax, pattern matching,
compilation) - other parts (code loading, starting the system
etc. have had dozens of hours work and are pretty near to the first
This reflects priorities - saving the programmer a few minutes typing
has never been a priority - making fast pattern matching algorithms
etc. was and is, a priority.
Today changing code.erl and error_handler.erl is pretty tricky for
reasons of backwards compatibility - it's easy to write a new code
loader, but it's really difficult to write one that is *beautiful*
and *backwards compatible*.
I've actually changed my mind on backwards compatibility - I'm
overjoyed when I find that 20 year old Erlang programs still run
flawlessly. Unlike <no names here> which changes so fast that even
programs written a few weeks ago no longer work.
On Fri, Feb 19, 2016 at 1:54 PM, zxq9 <> wrote:
> On 2016年2月19日 金曜日 13:39:44 Benoit Chesneau wrote:
>> > On 19 Feb 2016, at 12:32, Loïc Hoguin <> wrote:
>> > I've mentioned that at the beginning of the thread. We're going circles now. :-)
>> > I really don't think getting rid of the mandatory -module attribute is such a hard problem to solve...
>> for myself I am now wondering why we dont have a `-module` block and then ignoring the filename completely.
>> - benoit
> This is a much more interesting idea, imo.
> The reason I had referred to the filename == modname thing as a bandaid
> is because it appears to be the way the code loader was kludged into finding
> code -- because that's a workable and expedient solution, and writing an
> actual resource resolver is a lot heavier (but more correct) solution.
> erlang-questions mailing list
More information about the erlang-questions