[erlang-questions] calling a local function with an atom stored in a variable.

Robert Virding <>
Fri Aug 27 01:26:09 CEST 2010


On 20 August 2010 21:35, Mazen Harake <> wrote:
>  On 20/08/2010 23:21, Mariano Guerra wrote:
>>
>> the last line " ExprF must be an atom or evaluate to a fun", why can't
>> ExprF evaluate to an atom? why ExprF = foo, ExprF() doesn't work but
>> ?MODULE:ExprF() does?
>
> Try:
>
> -module(m).
> -compile(export_all).
> foo() -> (bar()):(baz())().
> bar() -> m.
> baz() -> biz.
> biz() -> io:format("Hello World").
>
> Should work. Hope it sheds some light.

This works because you are making an external call to the function,
even if is the same module.

The main reason why you cannot evaluate to a local function at
run-time is because local calls to functions in the same module are
resolved at compile-time and not at run-time. This for speed. Calls to
exported functions in other module, however, external calls, are
resolved at run-time which allows evaluating the module name and
function name at run-time.

Robert


More information about the erlang-questions mailing list