欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关于erlang:apply/2/3

程序员文章站 2022-03-10 08:07:36
...

 

 57 apply(Fun, Args) ->
 58     apply(Fun,Args).                        
 59 
 60 apply(Mod, Name, Args) ->
 61     apply(Mod, Name, Args).

 

 上面代码是erlang.erl文件里看到的。这里aplly/3方法里面又调用一次apply/3 请问是什么意思

老大: 在虚拟机里面实现的
langzhe: 谢谢
erlang.erl代码编译的时候 进行了特殊编译?所以再调用时候 不会默认调用erlang:apply/3?
老大:

需要进一步 看看是怎么编译的?

来自http://erlang.2086793.n4.nabble.com/Erlang-about-quot-apply-quot-function-in-the-erlang-erl-td3528347.html#a3531504的部分解释

apply is a built in function (BIF). It is implemented in C, not Erlang.

>
> apply is a built in function (BIF). It is implemented in C, not Erlang.
Does it work in a similar manner to that of Smalltalks, or differently?

In Smalltalks, `Boolean ifTrue: aBlock ifFalse: aBlock` literal forms
are usually compiled to call into native code directly, so the
implementation of this message in Smalltalk code in Smalltalk VMs looks
like this:

    ifTrue:trueBlock ifFalse:falseBlock
        ^self ifTrue:[ trueBlock call ] ifFalse: [ falseBlock call ]

which looks like it would not work, but when the form
`var ifTrue:aBlockLiteral ifFalse:aBlockLiteral` is encountered the
Smalltalk compiler takes over and replaces this with native code. So the
Smalltalk implementation is only ever hit for non-literal sends (either
non-literal blocks, or usage of selectors), and its only role is to
redirect execution to the native version.

Is the purpose (and behavior) of the pure version of erlang:apply
similar? 

 

----------引用Richard

It's a fairly common "bootstrapping" sort of thing.
Presumably
 - when the compiler sees apply/2 or apply/3 in an expression
   it generates special in-line code
 - there needs to be a "real" function definition so that it
   can be called indirectly itself, discoverable in the
   module exports, traced if calls arrive that way in the
   debugger, &c

 

 

apply的调用在beam加载的时候vm会调整相关的指令,实际上是在vm的opcode里面执行的。
专注高性能容错分布式服务器的研究和实现
http://blog.yufeng.info