博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Erlang 0053] fun & Code replacement
阅读量:5752 次
发布时间:2019-06-18

本文共 2197 字,大约阅读时间需要 7 分钟。

在  中,我们曾经看到过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  

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已经在运行的进程要想实现热更新也要使用完全限定的方式去的触发热更新,下面是  里面已经提到的一个实验:
 
%%%  版本一 -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.
相关: 

转载地址:http://umukx.baihongyu.com/

你可能感兴趣的文章
mint-ui loadmore组件注意问题
查看>>
Java中的volatile关键字
查看>>
MongoDB常用操作一查询find方法(转)
查看>>
Jquery chosen动态设置值 select Ajax动态载入数据 设置chosen和获取他们选中的值
查看>>
《深入理解计算机系统》阅读笔记--信息的表示和处理(上)
查看>>
[转] Windows局域网通过NTP设置时间同步
查看>>
移动端弹窗 layer.js 使用
查看>>
logback日志模板与详解
查看>>
Docker(一):Docker入门教程
查看>>
vue插件
查看>>
PHP如何动态传入参数
查看>>
【M22】考虑以操作符复合形式(op=)取代其独身形式(op)
查看>>
yield 关键字
查看>>
[转】.Net开发人员可以拥抱Entity Framework 了
查看>>
电影集合
查看>>
c# 汉字转拼音
查看>>
Win7输入法消失和不能切换的办法了
查看>>
[转]通过继承ConfigurationSection,在web.config中增加自定义配置
查看>>
使用UIImagePickerController时3DTouch引起Crash
查看>>
实时 Django 终于来了 —— Django Channels 入门指南
查看>>