[erlang-questions] Why do we need modules at all?

Jeff Schultz <>
Thu May 26 05:56:45 CEST 2011


On Tue, May 24, 2011 at 10:06:19AM +0200, Joe Armstrong wrote:
>    functions are really easy to reuse
>    modules are more difficult to reuse

Just to comment on this described contrast, consider a synthetic
example:

create(X) ->
    {classify(X), X}.

consume({C, X}) ->
    C = classify(X),
    io:format("~p~n", [X]).

classify(X) when X > 0 ->
    positive;
classify(X) when X < 0 ->
    negative.


This program has a bug, at least for our present purposes.
It's also rather pointless.  Imagine that classify/1 produced a
cryptographically strong message authenticator instead, if desired.


Now, normally, we'd put the three functions in a module, say 'm', and
call them from another module thus

test(X) ->
    W = m:create(X),
    m:consume(W).


When we fix the bug in classify/1 by adding a clause

classify(0) ->
    zero;

and reload m, we can now call test(0) successfully.  If, instead, we
have each function living in isolation in a global namespace, we might
be able to get away with just reloading classify/1, but we didn't even
know we were using it!  If we reload create/1, we just move the crash
to the consume/1 call.

See, modules are easy to reuse, and functions are hard ;-)


The thing we need to keep from modules as they are now is this
connection between groups of functions that must be used together.  I
think that if we wrote the example instead as

    let classify(X) . . .
    in create(X) . . .
    and consume(X) . . . .

we're just providing an alternate syntax for the existing module
definition in which the module name has become implicit.


Joe's proposal sounds fun, and might work very well once people
adjust, but the metadata is going to have to be worked very hard, and
Erlang's remote expression will have to use it too. 


    Jeff Schultz



More information about the erlang-questions mailing list