关于erlang:apply/2/3

本文探讨了Erlang中apply函数的工作原理及其在编译时如何被优化。通过内置函数(BIF)实现,实际调用过程由虚拟机直接处理而非通过Erlang代码。此外,还对比了Smalltalk中的类似机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值