[erlang-questions] Any way to dynamically access Records?

Angus Gratton agratton@REDACTED
Tue Oct 20 02:27:00 CEST 2009


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-functions-with.html

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


More information about the erlang-questions mailing list