[erlang-questions] how to dynamically create function to run in a different node

Ulf Wiger ulf@REDACTED
Thu Dec 25 20:29:39 CET 2014

On 25 Dec 2014, at 11:55, Dror Mein <drormein@REDACTED> wrote:

> My preferred solution would be somehow to compile a function turning it into <erl_eval> in a module. Is it possible? will it work?

One solution to this can be found in the locks application:


The code is invoked via a parse transform:

And the transform expands the pseudo-call `locks_watcher(self())`

watch_node(N) ->
    {M, F, A} =
        locks_watcher(self()),  % expanded through parse_transform
    P = spawn(N, M, F, A),
    erlang:monitor(process, P).

The {M, F, A} tuple will be {erl_eval, exprs, [Exprs, Bindings]}, where
Exprs is the abstract-form representation of the code for

Another version can be found in the deqc application:

where the function disconnect_script(Node) ends up returning a list of abstract forms via some parse transform magic.

disconnect_script(EqcNode) ->
      fun() ->
	      Nodes = nodes(),
	      [net_kernel:disconnect(N) || N <- Nodes -- [{'$var', EqcNode}]]

The function codegen:exprs/1 is a pseudo function handled by parse_trans_codegen.

Ulf W

Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141225/89222fac/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 496 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20141225/89222fac/attachment.bin>

More information about the erlang-questions mailing list