[erlang-questions] Restrictions on records

Ralf Wolter <>
Sun Mar 29 12:00:47 CEST 2009


Hello,

While working with records I ran into some unexpected restrictions. I'm
still learning Erlang so unexpected errors are to be expected, but this time
I really expected it to work. I tried to access the return of a function as
a record but Erlang responded with a syntax error before '#'.

The test code:

-module(test).
-export([
%    do/3
    do1/3,
    do2/3]).

-record(rec, {last_change, name=bla, value=0.0}).

update(Rec) -> Rec#rec{last_change=erlang:universaltime()}.

%do(Rec, Name, Setting) -> update(Rec)#rec{name=Name, value=value(Setting)}.
do1(Rec, Name, Setting) -> R = update(Rec), R#rec{name=Name,
value=value(Setting)}.
do2(Rec, Name, Setting) -> update(Rec#rec{name=Name, value=value(Setting)}).

value(high) -> 0.9;
value(moderate) -> 0.5;
value(low) -> 0.01.

The do function doesn't seem to work, while do1 and do2 have no problem. I'm
aware records are removed at compile time, but I guess I expected it to work
by replacing "update(Rec)#rec{name=Name, value=value(Setting)}" with
something like "{rec, C, N, V} = update(Rec), {rec, C, Name,
value(Setting)}". Obviously it does something else.

This sort of leads me to the question of how records actually work so I
could make better predictions on what is possible and what isn't. How are
they translated in to valid code by the compiler?

Ralf
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090329/bc885030/attachment.html>


More information about the erlang-questions mailing list