Functions in data structures

Ulf Wiger <>
Mon Jun 16 15:09:59 CEST 2003


On Mon, 16 Jun 2003, Joachim Durchholz wrote:

>Hi all,
>
>I've been toying with Erlang, preparing for a
>Real-World(TM) project, and such a state provokes a
>specific kind of questions.
>
>Mine are related to functions in data structures (I gather
>that Erlangese for "data structure" is "term" - is this
>correct?)
>
>Assume that a term contains a function. Further assume that
>this term is converted to binary, transmitted to a
>different machine, and converted back to an Erlang term; or
>assume it is stored in Mnesia and later read back into
>Erlang. The base line is: the term is taken out of the
>Erlang run-time system and later reconstructed. The
>question is whether the function can be called after
>reconstructing the term, under various circumstances:

The main issue when storing metadata persistently is how to
deal with upgrades. An issue when sending metadata to
another machine is whether that machine has the same code
loaded.

>1) Does it work if the function is a named function?

Yes, given that the named module is loaded on that machine.


>2) Does it work if the function is a named function, but the
>    term is restored in an environment that doesn't contain
>    that function?
>    (E.g. the term is sent over the network and reconstructed
>    in an Erlang run-time that doesn't have the same sources.)

No. Worst case (?) is that the named function exists, but
does something entirely different from what you expected.


>3) Does it work if the function is anonymous?

Anonymous functions are also tied to modules, so the same
answer as above applies, except anonymous functions may also
break if the module is replaced with an equivalent, but
heavily reorganized module. This will not break (1).

>4) Does it work if the function was constructed at run-time?
>    (Most functional languages can construct functions at run-time
>    by taking a given function and filling in a parameter with
>    either a term or another function. I'm not sure that this is
>    possible in Erlang, but I sincerely hope so...)

Anonymous functions are constructed at runtime (the logic is
static, but the closure is defined at runtime.) So the
answer is the same as for (3).

There is another option, seldomly exercized: you can send an
abstract form and evaluate it on the other side. This is
more stable in time and space than the other option, but
at the cost of slightly worse performance. The performance
issue can be partly overcome by on-the-fly compilation.

/Uffe
-- 
Ulf Wiger, Senior Specialist,
   / / /   Architecture & Design of Carrier-Class Software
  / / /    Strategic Product & System Management
 / / /     Ericsson AB, Connectivity and Control Nodes




More information about the erlang-questions mailing list