[erlang-questions] Creating records dynamically

Attila Rajmund Nohl <>
Thu Jan 21 14:30:14 CET 2010


2010/1/21, Kiran Khaladkar <>:
> hi all,
> I have record name in a variable how do i create variable of that.
> e.g.  (just a rough example of what the intention is)
> -record(sample, {a,b}).
> put_vals(T) ->
>        V = #T{a =1,b=2}.
>
> now when i call put_vals like
> put_vals(sample).
> it will fill the record and return the variable
> The values may be passed as list to put vals like [{a,1}, {b,2}]
>
> there is a way i found but want to know if there is any better way ..
> One can write several put_vals, one each record precisely.

I don't really understand what you want to achieve, but keep in mind
that the Erlang records are nothing but compile time hacks, internally
they are tuples, so your function would look like this:

put_vals(T) ->
  {T, 1, 2}.

Of course, this breaks if the internal representation of the records
change, and it's generally ugly.


More information about the erlang-questions mailing list