Any way to dynamically access Records?

Steve Davis <>
Tue Oct 20 14:35:44 CEST 2009


In the absence of mnesia to retrieve record field definitions, I
settled on a simple solution: namely adding the field names as the
first field in a record, e.g.:

-record(component, {fields=[id, class, body], id, class, body}).

For the case I was dealing with, the memory overhead of doing this was
not an issue, but YMMV.
/s


On Oct 19, 7:27 pm, Angus Gratton <> wrote:
> G.S. wrote:
> > Is there a way to get this to work: Row#TableName.Column
>
> > Where all are variables, dropped into the function.
>
> FWIW, I (along with many others, it seems) have experienced similar
> problems and found ways around them.
>
> There are some interesting existing solutions out on the net, like these:
>
> http://www.trapexit.org/Match_Specifications_And_Records_(Dynamically!)http://chlorophil.blogspot.com/2007/04/dynamic-record-access-function...
>
> Also, if you're just doing mnesia index lookups, mnesia:index_read()
> will take an indexed field name and look it up at runtime (with the
> associated performance hit.)
>
> What we ended up doing is code generating a lot of our records.
> Subsequently, we also now code generate a module with functions to look
> up arbitrary field values in records, and also functions to generate an
> arbitrary matchspec from a list of tuples {fieldname, fieldvalue}. This
> is a similar approach to the first link I posted above, although it's a
> bit more lo-fi because all the functions get code generated, instead of
> inferred by the parse transform.
>
> Hth.
>
> - Angus
>
> ________________________________________________________________
> erlang-questions mailing list. Seehttp://www.erlang.org/faq.html
> erlang-questions (at) erlang.org


More information about the erlang-questions mailing list