我的理解是-export()
可以在模块定义中公开一些但不是全部的函数。但是,在模块定义中,所有功能都可用。
我有一个看起来像这样的模块
-module(supervisor_test).
-export([start_listening/0, stop_listening/0, send_to_listener/1]).
listener() ->
receive
{Pid, Ref, x} ->
Pid ! {Ref, o};
{Pid, Ref, o} ->
Pid ! {Ref, x}
end.
supervisor() ->
process_flag(trap_exit, true),
Pid = spawn_link(?MODULE, listener, []),
register(reg_listener, Pid),
receive
{'EXIT', Pid, normal} -> % received when listener() finishes executing
ok;
{'EXIT', Pid, shutdown} -> % received when stop_listening() is called
ok;
{'EXIT', Pid, _} ->
supervisor()
end.
start_listening() ->
spawn(?MODULE, supervisor, []).
stop_listening() ->
Pid = whereis(reg_listener),
exit(Pid, shutdown).
send_to_listener(Value) ->
Ref = make_ref(),
reg_listener ! {self(), Ref, Value},
receive
{Ref, Reply} -> Reply
after 5000 ->
timeout
end.
每当我编译并调用supvervisor_test:start_listening()
时,我都会收到以下错误
=ERROR REPORT==== ... ===
Error in process ... with exit value:
{undef,[{supervisor_test,supervisor,[],[]}]}
如果我export_all
它就会消失并暴露一切。
我尝试编译
-module(test).
-export([f1/0]).
f1() ->
f2().
f2() ->
io:format("I am here!~n").
并致电test:f1()
并且工作正常。
答案 0 :(得分:3)
在start_listener()
中,您正在调用spawn()
的MFA版本。这将使用apply()
和apply docs状态:“必须从Module中导出应用的函数。”