[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 
目录
相关文章
|
11月前
|
Ruby
浅谈Ruby中的block, proc, lambda, method object的区别
浅谈Ruby中的block, proc, lambda, method object的区别
61 0
|
11月前
|
Ubuntu C语言
【ubuntu】2.c:(.text+0xd2): undefined reference to `pthread_create‘ collect2: error: ld returned
【ubuntu】2.c:(.text+0xd2): undefined reference to `pthread_create‘ collect2: error: ld returned
103 0
|
12月前
|
程序员 Go Windows
【go 语言】解决 grpc:--proto_path passed empty directory name. (Use "." for current directory.)
【go 语言】解决 grpc:--proto_path passed empty directory name. (Use "." for current directory.)
142 0
|
消息中间件 PHP
laravel6 使用rabbitmq报错:Call to a member function make() on null at Queue\\Jobs\\Job.php:215
laravel6 使用rabbitmq报错:Call to a member function make() on null at Queue\\Jobs\\Job.php:215
138 0
relocation R_X86_64_PC32 against symbol lua_newstate can not be used when making a shared object
relocation R_X86_64_PC32 against symbol lua_newstate can not be used when making a shared object
210 0
安装 xgboost 报错ERROR: Command "python setup.py egg_info" failed with error code 1 in /private/var/fold
安装 xgboost 报错ERROR: Command "python setup.py egg_info" failed with error code 1 in /private/var/fold
安装 xgboost 报错ERROR: Command "python setup.py egg_info" failed with error code 1 in /private/var/fold
Python bug:ValueError: invalid literal for int() with base 10: ''
Python bug:ValueError: invalid literal for int() with base 10: ''
|
Java 编译器 Go
Golang 笔记(一):值方法和指针方法(Value Methods vs Pointer Methods)
Golang 笔记(一):值方法和指针方法(Value Methods vs Pointer Methods)
130 0
|
PHP
【laravel】call_user_func_array在框架的使用
【laravel】call_user_func_array在框架的使用
179 0
【laravel】call_user_func_array在框架的使用