Re: maps inside records

Björn Gustavsson <[email protected]>
Newsgroups gmane.comp.lang.erlang.bugs
Message-ID <CA+yh78QxiBDzf8U+2ePGi86-hgN12bigTe=auO1qnKQ7-SoDTw@mail.gmail.com>
On Sun, Feb 1, 2015 at 9:47 AM, Sergej Jurecko <[email protected]> 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
_______________________________________________
erlang-bugs mailing list
[email protected]
http://erlang.org/mailman/listinfo/erlang-bugs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.