[erlang-questions] Invoking a function based on name only

Richard Carlsson richardc@REDACTED
Fri Sep 14 16:40:59 CEST 2007

Bjorn Gustavsson wrote:
> The compiler was changed in one of the R11B releases to
> internally rewrite apply(M, Foo, [A1,A2,An]) to M:Foo(A1, A2, An);
> thus, exactly the same code is produced and you can choose the
> version you find more readable (personally, I find M:Foo(A1, A2, An)
> more readable).

Just to clarify, this only happens when the length of the argument
list is constant at compile time, as in apply(M, Foo, [A1,A2,An]).

If you write apply(M, Foo, Args), where Args is some variable or other
expression that is not a constant list, like [A1,A2|ExtraArgs], the
compiler cannot decide the arity of the call at compile time, and will
generate a full 'apply'. (Apply has to traverse the list at runtime to
prepare for the call, which is why it is less efficient.)


More information about the erlang-questions mailing list