[Erlang 0053] fun & Code replacement

简介:
在  [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 
目录
相关文章
|
4月前
|
网络协议 C++
解决MASM32代码汇编出错: error A2181: initializer must be a string or single item
解决MASM32代码汇编出错: error A2181: initializer must be a string or single item
|
6月前
|
开发者 Python
【Python】已解决:TypeError: __init__() got an unexpected keyword argument ‘port’
【Python】已解决:TypeError: __init__() got an unexpected keyword argument ‘port’
938 0
【Python】已解决:TypeError: __init__() got an unexpected keyword argument ‘port’
|
5月前
解决Flutter报错The named parameter |method ‘xxxx‘ isn‘t defined.
解决Flutter报错The named parameter |method ‘xxxx‘ isn‘t defined.
233 3
|
6月前
|
Web App开发 测试技术 API
【Python】已解决:TypeError: *init*() got an unexpected keyword argument ‘firefox_options’
【Python】已解决:TypeError: *init*() got an unexpected keyword argument ‘firefox_options’
118 0
|
6月前
|
Python
【Python】已解决:TypeError: *init*() missing 1 required positional argument: ‘scheme’
【Python】已解决:TypeError: *init*() missing 1 required positional argument: ‘scheme’
417 0
|
6月前
|
机器学习/深度学习 Python
【Python】已解决TypeError: init() got an unexpected keyword argument ‘threshold’
【Python】已解决TypeError: init() got an unexpected keyword argument ‘threshold’
331 0
CodeBlocks中运行出现undifined reference to std::cxxll:basic_string错误解决方案
CodeBlocks中运行出现undifined reference to std::cxxll:basic_string错误解决方案
572 0
CodeBlocks中运行出现undifined reference to std::cxxll:basic_string错误解决方案
|
Android开发 Kotlin
kotlin协程库报错“Program type already present”解决
最近在学习kotlin,学习到协程库这一块了,针对Android的话就是coroutines-android库。本来学习就不容易了,再加上kotlin现在还处于快速变化期,那个酸爽简直了,废话不多说,进入正题。
Python typeError: a bytes-like object is required, not ‘str’ Solution
Python typeError: a bytes-like object is required, not ‘str’ Solution
|
Android开发 Kotlin
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )
289 0
【错误记录】Kotlin 编译报错 ( Not nullable value required to call an ‘iterator()‘ method on for-loop range )