Defining records at runtime

orbitz@REDACTED orbitz@REDACTED
Thu Apr 6 02:34:22 CEST 2006


Well a record is really just a tuple where the first element is the  
name of the record. The compiler just does the mapping for you for  
extracting the elements by name.

On Apr 5, 2006, at 2:47 PM, tty@REDACTED wrote:

> Hello folks,
>
> Is there a way of defining records at runtime which usable by QLC ?  
> I currently allow the user to define mnesia tables on the fly. e.g:
>
>      new_table(Table, Attrs) ->
>          mnesia:create_table(Table, [... {attributes, Attrs}]).
>
>      new_entry(Table, Vals) ->
>          Record = list_to_tuple([Table] ++ Vals),
>          mnesia:dirty_write(Record).
>
> However while doing a query I can not write:
>
>      query(Table, Attr, MatchVal) ->
>          QH = qlc:q([P || P <- mnesia:table(Table), P#Table.Attr ==  
> MatchVal]),
>          ...
>
> since the Table record does not actually exist. I get around that  
> by finding the
> index of Attr and doing 'elements(Index, P) == MatchVal' instead.  
> It would simplify my code if I could define records at runtime and  
> avoid having to keep track of the location of a particular attribute.
>
> Any ideas ?
>
> Thanks
>
> t




More information about the erlang-questions mailing list