Why special function apply(Mod, Func, Args).

Ulf Wiger <>
Fri Feb 7 14:20:36 CET 2003


On Fri, 7 Feb 2003, Suresh S wrote:

>Hi,
>
>In erlang Course there is a special function described
>i.e apply(Mod, Func, Args) at
>http://www.erlang.org/course/course.html
>
>Special Function :
>
>apply(Mod, Func, Args)
>
>1> apply( lists1,min_max,[[4,1,7,3,9,10]]).
>    {1, 10}
>
>but this will solve the problem
>
>2> lists1:min_max([4,1,7,3,9,10]).
>    {1, 10}
>
>i really do not understand why the special function
>apply(Mod, Func, Args).


The apply/3 function was the original way to do "meta
programming" in Erlang. A typical example is

	rpc:call(Node, Module, Function, Arguments)

which results in a message to the process 'rex' on Node,
instructing it to call

	apply(Module, Function, Arguments)

and return the result.


Nowadays, Erlang supports constructs like

	Module = lists1,
	Module:min_max([4,1,7,3,9,10])

or

	Module = lists1,
	Function = min_max,
	Module:Function([4,1,7,3,9,10]).

which are "nicer" than apply/3. Also, function objects
perform much the same task, but are nicer still. (:

The OTP behaviours, like gen_server, gen_event, et al,
rely heavily on meta programming. For example, gen_server
does the following in principle:

	receive
	   {'gen_call',From,Request} ->
	      case catch Module:handle_call(
	                    Request, From, ModState) of
	         {reply, Reply, NewModState} ->
	            ...
	      end;
	         ...
	end.

/Uffe
-- 
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson Telecom AB, ATM Multiservice Networks




More information about the erlang-questions mailing list