[erlang-bugs] maps inside records

Björn Gustavsson bjorn@REDACTED
Mon Feb 2 08:04:47 CET 2015


On Sun, Feb 1, 2015 at 9:47 AM, Sergej Jurecko <sergej.jurecko@REDACTED> wrote:
> Tested in 17.3 and 17.4
>
> Incorrect behavior 1:
>
> -record(ev,{info}).
> test() ->
>         test(#ev{info = #{type => dir, name => "My folder"}}).
> test(#ev{info = #{type := dir} = I} = E) ->
>         io:format("E = ~p~n",[E]),
>         io:format("E#event.info = ~p~n",[E#ev.info]),
>         io:format("I = ~p~n",[I]).
>
> Calling test/0 will print:
> E = {ev,#{name => "My folder",type => dir}}
> E#event.info = #{type => dir}
> I = #{name => "My folder",type => dir}
>
> Why is E#event.info without name field?
>

The behaviour is the expected.

Records are compile-time construct that are translated
to tuples. So at run-time the record is a tuple and
will be printed as a tuple.

>
> Incorrect behavior 2:
> test1() ->
>         Z = #{type => dir,name => "My folder"},
>         io:format("Z = ~p ~n",[Z]),
>         P = #ev{info = Z},
>         io:format("P = ~p ~n",[P]),
>         test1(P).
> test1(#ev{info = #{type := dir, name := Name}} = E) ->
>         io:format("E = ~p ~n",[E]),
>         E#ev.info.
>
> Calling test1/0 will print:
>
> ecds:test1().
> Z = #{name => "My folder",type => dir}
> P = {ev,#{name => "My folder",type => dir}}
> E = {ev,#{name => "My folder",type => dir}}
> ** exception error: bad argument
>      in function  ecds:test1/1
>
> Exception happened in second header of test1, yet E was printed.
>

An unsafe optimisation causes that strange behavior.

The correction has already been merged to the
'maint' branch and will be included in the next
maintenance release.

/Bjorn

-- 
Björn Gustavsson, Erlang/OTP, Ericsson AB



More information about the erlang-bugs mailing list