Parameterized/Abstract modules

Richard Carlsson <>
Fri Mar 11 21:50:07 CET 2005


Mark Scandariato wrote:
> Perhaps (along the lines of Vlad's suggestion yesterday) the compiler
>  could notice that functions that don't reference the module's
> parameters (or THIS) should be exported as "static"?

The short answer is no. Consider the following:

   -module(foo, [A]).
   -export([f/1]).

   f(X) -> 1 + g(X).

   g(X) -> h(X) - 1.

   h(X) -> X + A.

Neither f nor g directly access A (or THIS), so both would be candidates
for the sort of thing you suggest.

However, h/1 needs access to THIS (or at least to A), so that must be
passed through g/1, and must enter the module via the exported f/1
(even though this calling convention is handled "under the hood" for
parameterized modules, and you normally don't need to think about it).

So, to find those functions that could be automatically exported as
static (i.e., not taking an implicit THIS parameter), you'd have to
take the transitive closure over the functions which could access THIS,
and could then remove the unnecessary THIS parameter from all the
remaining functions (if any).

I had actually indended to implement this as an optimization, but I
just didn't have the time. It would be all right for non-exported
functions, since these are not part of the external interface. But
doing it for exported functions would mean that a particular function
could switch unpredictably back and forth between being "static" and
being a "member", simply depending on whether or not some indirectly
called function could need access to the THIS parameter. So I don't
think it's a good idea to let the compiler decide this property.

	/Richard



More information about the erlang-questions mailing list