What kind of types can be sent on an Erlang message?

后端 未结 3 2012
清酒与你
清酒与你 2020-12-29 15:29

Mainly I want to know if I can send a function in a message in a distributed Erlang setup.

On Machine 1:

F1 = Fun(         


        
3条回答
  •  伪装坚强ぢ
    2020-12-29 16:02

    You can send any valid Erlang term. Although you have to be careful when sending funs. Any fun referencing a function inside a module needs that module to exist on the target node to work:

    (first@host)9> rpc:call(second@host, erlang, apply,
                            [fun io:format/1, ["Hey!~n"]]).
    Hey!
    ok
    (first@host)10> mymodule:func("Hey!~n").
    5
    (first@host)11> rpc:call(second@host, erlang, apply,
                             [fun mymodule:func/1, ["Hey!~n"]]).
    {badrpc,{'EXIT',{undef,[{mymodule,func,["Hey!~n"]},
                            {rpc,'-handle_call_call/6-fun-0-',5}]}}}
    

    In this example, io exists on both nodes and it works to send a function from io as a fun. However, mymodule exists only on the first node and the fun generates an undef exception when called on the other node.

提交回复
热议问题