[erlang-questions] Creating records dynamically

黃耀賢 (Yau-Hsien Huang) <>
Thu Jan 21 14:47:57 CET 2010


For a remark:

-module(test).
-record(sample, {a, b}).

test(T) ->
    case put_vals(T) of
        #sample{a=X, b=Y} -> [sample, X, Y];
Any -> Any
    end.

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

When executing,

> test:test(a).
{a,1,2}
> test:test(sample).
[sample,1,2]


On Thu, Jan 21, 2010 at 9:30 PM, Attila Rajmund Nohl <
> wrote:

> 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.
>
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org
>
>


More information about the erlang-questions mailing list