Skip to content

Instantly share code, notes, and snippets.

@elbrujohalcon
Created October 31, 2011 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elbrujohalcon/1328116 to your computer and use it in GitHub Desktop.
Save elbrujohalcon/1328116 to your computer and use it in GitHub Desktop.
Tricky stuff with records
-module(record).
-record(rec, {field1, field2}).
-export([test1/0, test2/0, test3/0, test4/0]).
%% First I wrote something like this...
test1() -> [#rec{field1 = 1, field2 = 1}
#rec{field1 = 2, field2 = 2}].
%% ...this compiles without a warning and then, when I run it...
%% > record:test1().
%% [#rec{field1 = 2,field2 = 2}]
%% WHAT? (o_O) Why are you ignoring my first record? (¬_¬)
%% Oooooh! I was missing a comma!!
test2() -> [#rec{field1 = 1, field2 = 1},
#rec{field1 = 2, field2 = 2}].
%% ...now it works
%% > record:test2().
%% [#rec{field1 = 1,field2 = 1},#rec{field1 = 2,field2 = 2}]
%% ...but why was it compiling at all?
%% ...When I finally figured it out, I tried with the following function...
test3() -> [#rec{field1 = 1}
#rec{field2 = 2}].
%% ...and with this one is more evident...
%% > record:test3().
%% [#rec{field1 = 1,field2 = 2}]
%% And with this one even more...
test4() -> [#rec{field1 = 1}#rec{field2 = 2}].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment