[erlang-questions] Restrictions on records

Michal Zajda <>
Sun Mar 29 13:56:35 CEST 2009


try the following code:

-module(l).
-export([
     start/0,
     do/3,
     do1/3,
     do2/3]).

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

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

start() ->
    X = #rec{},
    do(X,"xx",high).


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.


2009/3/29 Ralf Wolter <>

> 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
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090329/b1ec99a8/attachment.html>


More information about the erlang-questions mailing list