[erlang-questions] Is it a compiler bug?

Stanislaw Klekot <>
Thu Apr 13 15:39:26 CEST 2017


On Thu, Apr 13, 2017 at 01:27:40PM +0000, Minin Maxim wrote:
> i'm confused about a very simple module like this:
> -----------------------------------------------------------------------
> -module(sample).
> 
> -record (a, {field1}).
> 
> -export([bug/0]).
> 
> bug() ->
>                 [
>                 #a{field1 = 1} %% COMMA IS MISSING
>                 #a{field1 = 2},
>                 #a{field1 = 3}
>                 ].
> -----------------------------------------------------------------------
> 
> Why can it be compiled?
[...]
> 2> sample:bug().
> [{a,2},{a,3}]

Apparently the compiler takes the first #a{} record as value X, and then
replaces in X field1. Let's complicate your example a little:

#v+
-module(sample).

-record (a, {field1, field2}).

-export([bug/0]).

bug() ->
                [
                #a{field1 = 1, field2 = "foof"} %% COMMA IS MISSING
                #a{field1 = 2},
                #a{field1 = 3}
                ].
#v-

Note that with the missing comma you'll get a list of {a,1,"foof"},
{a,2,undefined}, and {a,3,undefined}, and with missing comma you'll get
list of {a,2,"foof"}, {a,3,undefined}.

Or a different view on your code: "#a{field1 = 1} #a{field1 = 2}" can be
read as "(#a{field1 = 1})#a{field1 = 2}", or in two steps:
"X = #a{field1 = 1}, X#a{field1 = 2}".

-- 
Stanislaw Klekot


More information about the erlang-questions mailing list