[erlang-questions] record type as variable

Kresten Krab Thorup krab@REDACTED
Thu Aug 5 10:07:44 CEST 2010


A simple macro will also do ...

---------------------------
-module(foo).
-record(x,{name,zz}).
-record(y,{yy,name}).
-export([test1/0,test2/0]).
-define(create(Type,Name),#Type{name = Name}).

test1() ->
  ?create(x,"Noel").
test2() ->
  ?create(y,"Noel").
---------------------------

Eshell V5.8  (abort with ^G)
1> c(foo).
{ok,foo}
2> foo:test1().
{x,"Noel",undefined}
3> foo:test2().
{y,undefined,"Noel"}






On Aug 4, 2010, at 19:05 , Noel Bush wrote:

> Is there any way to specify the type of a record using a variable? I
> want to be able to do this sort of thing:
> 
> create(Type, Name) ->
>    #Type{name = Name}.
> 
> (Obviously, I'm responsible for making sure that Type corresponds to a
> record type, that any record type has a name field, etc.) It doesn't
> seem like that much of a pain to write a few record-handling functions
> that would allow me to do this, but it would be great to find out that
> someone's already done the work.
> 
> Thanks,
> Noel
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
> 

Kresten Krab Thorup, CTO, Trifork



More information about the erlang-questions mailing list