Lists, Maps and Commas – Erlang Battleground

Datetime:2017-04-19 05:19:22         Topic: Erlang          Share        Original >>
Here to See The Original Article!!!

Lists, Maps and Commas

This will be a super-quick post about something that came up at least twice in the last few months within the Erlang community. It’s related to the syntax of maps within a list.

(original image from Good Free Photos )

How Many Maps?

Quick question: how many maps do we have in the following list?

[#{a => 1},
 #{b => 2}
 #{c => 3}].
  • That doesn’t even compile!

People following the erlang-questions mailing list might know already, but the correct answer is not 3, nor a compile error. It’s 2 .

Why not 3?

Well, there is a missing comma in the second row.

But why does it compile, then?

Because what’s written there is a perfectly valid expression, of course. And it matches the syntax used for records, as it’s pointed out in this thread :


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


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

that function on the shell returns a list with 2 elements, as expected…

1> rr(sample).
2> sample:bug().
[#a{field1 = 2,field2 = "foof"},
 #a{field1 = 3,field2 = undefined}]