Record selectors

Matthias Lang matthias@REDACTED
Fri Jan 3 14:38:54 CET 2003


 Daniel> I'm curious as to why it is necessary to specify the record
 Daniel> name when selecting a record instance bound to a variable,
 Daniel> i.e., Variable#RecordName.Field.

 Chris> Because Erlang is a dynamically-typed language.  In a 
 Chris> dynamically-typed language, values have types, but variables 
 Chris> do not.

_If_ the runtime system had a table of {recordname, fieldname} -> index,
it would be possible to allow:

   P = #person{name = "Bruce", occupation = "language designer"},
   Name = P#name.

where P#name is sugar for record_field(P, name), which could be
implemented to do something like:

   record_field(Record, Field) ->
       {ok, Index} = magic_table_lookup(element(1, Record), Field),
       element(Index, Record).
       
There is no such table of record information AFAIK, so the above is
just speculation.

The cost? Zero for existing code. For code which uses the new feature
there'd be an extra lookup operation for each such field access.

There are some solvable wrinkles, e.g. handling the common case where
the same record is defined several times in a system. But the best
reason not to do it is fear of making the record mess worse than it
already is.

Matthias



More information about the erlang-questions mailing list