-
-
Save stephb9959/f4fb98caeaea76b7acfffd63a940939b 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 hidden or 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