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

Dustin Sallings <>
Fri Sep 14 08:23:34 CEST 2007


On Sep 13, 2007, at 22:24, igwan wrote:

> It is worth noting that if the number of arguments is known at  
> compile time, it's more efficient to use Module:Function(Arg1,  
> Arg2, ...) than apply/3.
> From the the Efficiency Guide : "The syntax |M:Foo(A1,A2,An)| (also  
> referred to as implicit apply, where M and Foo are bound variables)  
> where equivalent with |apply(M,Foo,[A1,A2,An])| in releases pre OTP- 
> R10B. The compiler will now optimize this syntax giving it better  
> performance than apply/3"

	Thanks.  I thought I saw something like that.  Revised:

1> M = list_to_atom("erlang").
erlang
2> F = list_to_atom("now").
now
3> M:F().
{1189,750920,871983}

or

4> {M, F} = {list_to_atom("erlang"), list_to_atom("now")}.
{erlang,now}
5> M:F().
{1189,750980,161945}

	Whichever is more appropriate in the given context.

> igwan
>
> Dustin Sallings a écrit :
>> On Sep 13, 2007, at 15:42 , Kevin A. Smith wrote:
>>
>>
>>> If I have the name of the function and the module it belongs to, how
>>> can I invoke it? I tried cobbling together erl_scan and friends but
>>> the embedded macros in the code gave me some heartburn. Any  
>>> pointers?
>>>
>>
>> 1> apply(list_to_atom("erlang"), list_to_atom("now"), []).
>> {1189,724492,627140}
>>
>>
>
>

-- 
Dustin Sallings





More information about the erlang-questions mailing list