Created
October 8, 2011 19:45
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%% 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). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://gist.github.com/1213721 for a macro/fun to make a record out of a proplist