在 [Erlang 0037] Erlang Parameterized Module 中,我们曾经看到过Erlang Parameterized Module执行了new之后返回的结果其实是一个tuple结构,例如:
 P=p:new(zen,23).
{p,zen,23}
创建的模块实例是通过{p,zen,23}这样一个tuple结构来表达的;类似的对于普通的方法也是使用类似的结构表达(按照认知先后规律这个顺序其实应该倒过来 ):
Eshell V5.8.2 (abort with ^G)
1> list_to_tuple([a,b,c]).
{a,b,c}
2> tuple_to_list({a,b,c}).
[a,b,c]
3>
4> F={lists,append}.
{lists,append}
5> F([1,2],[3,4]).
[1,2,3,4]
6> fun lists:append/2([a,b],[c,d]).
[a,b,c,d]
7>
 

  Myth: Funs are slow  http://www.erlang.org/doc/efficiency_guide/myths.html

Yes, funs used to be slow. Very slow. Slower than apply/3. Originally, funs were implemented using nothing more than compiler trickery, ordinary tuples, apply/3, and a great deal of ingenuity.

But that is ancient history. Funs was given its own data type in the R6B release and was further optimized in the R7B release.

Now the cost for a fun call falls roughly between the cost for a call to local function and apply/3.

 

Warning:

Tuples are not fun(s). A "tuple fun", {Module,Function}, is not a fun. The cost for calling a "tuple fun" is similar to that of apply/3 or worse. Using "tuple funs" is strongly discouraged, as they may not be supported in a future release, and because there exists a superior alternative since the R10B release, namely the fun Module:Function/Arity syntax.

 
    关于fun还有一个要注意的地方就是代码热替换,需要用完全限定方式(m:f)去调用一下才可以:
For code replacement of funs to work, the syntax fun Module:FunctionName/Arity should be used.
 
不仅仅是fun已经在运行的进程要想实现热更新也要使用完全限定的方式去的触发热更新,下面是 [Erlang 0010] Erlang 热更新 里面已经提到的一个实验:
 
%%%  版本一
-module(a).
-compile(export_all).


meta()->
this_is_version_10001.


r()->
receive
code_switch -> a:r();
Msg-> io:format("Msg:~p~n",[Msg]),r()
end.


%%% 版本二
-module(a).
-compile(export_all).


meta()->
this_is_version_2012.


r()->
receive
code_switch -> a:r();
Msg-> io:format("Now Msg:~p~n",[Msg]),r()
end.
   实验结果:
Eshell V5.9 (abort with ^G)
1> a:meta().
this_is_version_2012
2> P= spawn(a,r,[]).
<0.35.0>
3> P!abc.
Now Msg:abc
abc
4> c:l(a).
{module,a}
5> P2= spawn(a,r,[]).
<0.39.0>
6> a:meta().
this_is_version_10001
7> P2!abc.
Msg:abc
abc
8> P!code_switch.
code_switch
9> P!abc.
Msg:abc
abc
10>
对于fun的替换也可以这样:
loop(Fun, State) ->
receive
{From, {rpc, Tag, Q}} ->
{Reply, State1} = Fun(Q, State),
From ! {Tag, Reply},
loop(Fun, State1);
{code_upgrade, Fun1} ->
loop(Fun1, State)
end.

相关: Live Code Update in Erlang