Dynamically invoking unexported function

Dominic Williams xpdoka@REDACTED
Mon Jan 16 17:06:28 CET 2006


Hello,

I would like to invoke a local function dynamically in
a receive, like this:

  receive
    {Pid, Request, Arguments} ->
      ?MODULE:Request(Arguments);    % (*)
    ...
  end,

which would invoke e.g.

foo({X,Y}) ->
  ...

Unfortunately, (*) only seems to work on exported
functions. Is that correct, and if so what is the
rationale? If not, what am I missing?

I came up with this to avoid exporting the functions
that get called dynamically:

  receive
    {Pid, Request, Arguments} ->
      F = lookup (Request),  % (**)
      F(Arguments);
    ...
  end,

lookup (foo) ->
  fun ({X,Y}) ->
     ...
  end;
lookup (bar) ->
  ...

Apart from the slightly more verbose syntax, are there
any serious downsides to this approach?

(**) fun module:function/parity also only works on
exported functions...

Cheers,

Dominic Williams
http://www.dominicwilliams.net

----




More information about the erlang-questions mailing list