Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Created October 8, 2011 19:45
Show Gist options
  • Save gdamjan/1272771 to your computer and use it in GitHub Desktop.
Save gdamjan/1272771 to your computer and use it in GitHub Desktop.
An erlang macro that creates a function to convert a record to a key-value list
%%% This macro will create a function that converts a record to
%%% a {key, value} list (a proplist)
-define(record_to_list(Record),
fun(Val) ->
Fields = record_info(fields, Record),
[_Tag| Values] = tuple_to_list(Val),
lists:zip(Fields, Values)
end
).
-record(rec, {bar = "baz", camp = "spam"}).
test() ->
Record = #rec{bar = "qux"},
Fun = ?make_record_to_list(rec),
Fun(Record).
@gdamjan
Copy link
Author

gdamjan commented Oct 10, 2011

See also https://gist.github.com/1213721 for a macro/fun to make a record out of a proplist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment