[erlang-questions] Variable bindings

Aggelos Giantsios aggelgian@REDACTED
Sat Jan 26 17:16:14 CET 2013


On Sat, Jan 26, 2013 at 4:32 PM, Tyron Zerafa <tyron.zerafa@REDACTED>wrote:

> Does there exist any particular function which would give me all variable
> bindings (including their values) at that point.
>
>
Hello!!

I don't think that it's possible to retrieve such information when running
compiled code. After all the transformations and optimizations are
performed by the compiler, the actual variables in the program may be
different form those declared in the source code.

For example, consider this code:

foo(X) ->
    Y = X,
    max(Y, 42).

The value of Y will most surely be propagated to its use in max/2 thus
eliminating the variable from the final compiled code altogether.

So all you could theoretically access at runtime would be the actual
variables in the function's call frame in the stack. However, this is done
by the erts emulator and I don't believe this is possible from within
Erlang code.

One way to work around this would be to manually store the bindings, for
example, in a dictionary so you can perform lookups at will. However, that
means that there would be overhead due to the stores and lookups and also
require more memory.

Aggelos
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130126/3c4a09ea/attachment.htm>


More information about the erlang-questions mailing list