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

后端 未结 3 2016
清酒与你
清酒与你 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条回答
  •  Happy的楠姐
    2020-12-29 15:49

    Here's an interesting article about "passing fun's to other Erlang nodes". To resume it briefly:

    [...] As you might know, Erlang distribution works by sending the binary encoding of terms; and so sending a fun is also essentially done by encoding it using erlang:term_to_binary/1; passing the resulting binary to another node, and then decoding it again using erlang:binary_to_term/1.[...] This is pretty obvious for most data types; but how does it work for function objects?

    When you encode a fun, what is encoded is just a reference to the function, not the function implementation. [...]

    [...]the definition of the function is not passed along; just exactly enough information to recreate the fun at an other node if the module is there.

    [...] If the module containing the fun has not yet been loaded, and the target node is running in interactive mode; then the module is attempted loaded using the regular module loading mechanism (contained in the module error_handler); and then it tries to see if a fun with the given id is available in said module. However, this only happens lazily when you try to apply the function.

    [...] If you never attempt to apply the function, then nothing bad happens. The fun can be passed to another node (which has the module/fun in question) and then everybody is happy. Maybe the target node has a module loaded of said name, but perhaps in a different version; which would then be very likely to have a different MD5 checksum, then you get the error badfun if you try to apply it.

    I would suggest you to read the whole article, cause it's extremely interesting.

提交回复
热议问题