[erlang-questions] What is the function be defined as BIF?

Håkan Huss huss01@REDACTED
Tue Nov 27 20:30:47 CET 2007


On Nov 27, 2007 8:17 AM, LUKE <luke@REDACTED> wrote:
>
>
> I found that i lose some word in the code.
>
> start(Name,Mod)->register(Name,spawn(fun()->loop(Name,Mod,Mod:init()) end)).
>
> But
>
> Why  the code can be compile?
>
> start(Name,Mod)->erlang:register(spawn(fun()->loop(Name,Mod,Mod:init())
> end)).
>
It can be compiled, but it will cause an undef-exception when run. The
reason is that
in the first case, the compiler catches the error since register must
either be a local
function or a BIF. Since there is no local function register/1, and no
BIF register/1, the
compiler complains.

In the second case, the compiler cannot know what functions are
exported from the
erlang module. Thus, the call to erlang:register/1 is accepted, and it
is only when it
is performed that we can know whether it actually exists.

Regards,
/Håkan
>
>
>
> ----- Original Message -----
> From: LUKE
> To: erlang-questions@REDACTED
> Sent: Tuesday, November 27, 2007 2:50 PM
> Subject: What is the function be defined as BIF?
>
>
> -module(genserver).
> -export([start/2,rpc/2]).
>
> start(Name,Mod)->register(spawn(fun()->loop(Name,Mod,Mod:init()) end)).
> ....
>
> Eshell V5.5.5  (abort with ^G)
> ...
> 9>c(genserver).
> ./genserver.erl:4: function register/1 undefined
>
> --
> modify the register to  erlang:register
> start(Name,Mod)->erlang:register(spawn(fun()->loop(Name,Mod,Mod:init())
> end)).
> --
>
> 10> c(genserver).
> {ok,genserver}
>
> The register function is BIF ?
> And the spawn function is BIF?
> All of erlang:* is BIF?
> When we need specify a prefix erlang:* ?
> Why the most of example does not using erlang:register?
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



More information about the erlang-questions mailing list