Like the other answers, you need to flip your own solution to accomplish this. However, the proposed solutions are incomplete. For example, it does not consider default values โโfor record entries. I use the following code snippet to take care of this conversion:
%% @doc returns a "RECSPEC" that can be used by to_rec in order to %% perform conversions -define(RECSPEC(R), {R, tuple_to_list(#R{}), record_info(fields, R)}). %% @doc converts a property list into a record. -spec to_rec(recspec(), proplist()) -> record(). to_rec({R, [_ | N], Spec}, P) when is_atom(R) and is_list(Spec) -> list_to_tuple( [R | lists:foldl( fun ({K,V}, A) -> case index_of(K, Spec) of undefined -> A; I -> {Head, Tail} = lists:split(I, A), Rest = case Tail of [_ | M] -> M; [] -> [] end, Head ++ [V | Rest] end end, N, P)]).
Now you can simply do:
-record(frob, {foo, bar="bar", baz}). to_rec(?RECSPEC(frob), [{baz, "baz"}, {foo, "foo"}])
what gives
#frob{foo="foo", bar="bar", baz="baz"}
I put this in a small โtoolkitโ library that I am going to compile for these small โsnippetsโ that just make life easier when developing Erlang applications: ETBX
source share