[erlang-questions] Creating records dynamically

Garrett Smith g@REDACTED
Fri Jan 22 03:32:47 CET 2010


On Thu, Jan 21, 2010 at 5:00 AM, Kiran Khaladkar
<kiran.khaladkar@REDACTED> wrote:
> 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.

The dynamic behavior you're looking for is generally accommodated by
proplists, though I presume you have a reason for using records.

You might also create a module that provides an API for the data
you're looking to manage. In that case you can hide the implementation
(use records, dicts, whatever).

Garrett


More information about the erlang-questions mailing list