records

Hakan Stenholm <>
Mon Dec 3 16:58:33 CET 2001


The imidiat problem can be solved by adding a guard 
"foo(A, ...) when record(A, ...) ->" to do the type checking.

This entier problem is realy due to the fact that records are not treated as a 
unique data type inside the VM (virtual machine) - they are implemented as
tuples. This makes it impossible to distinguish between a record and a tuple 
that happens to look like a record, this is also a problem if you have a
function like this:

% this matches both for a tuple and a record (as record is realy a special 
% kind of tuple)
f(A) when tuple(A) -> .... ;
% this will never be run 
f(A) when record(A,foo_rec) -> ....; 

The code above must be rewritten as below to work porperly

f(A) when record(A,foo_rec) -> ....; 
f(A) when tuple(A) -> .... ;

It should be noted that all record accesses and updates end up as calls to 
element/2 and setelement/3 i.e. tuple manipulating operations, this is done by
the preprocessor during compilation.
Record fieldnames are converted to indexes by the preprocessor as well.

So when the code is actualy run, the VM has no idea that some tuples should
be typechecked as records rather than simple tuples, i.e. records are only 
"syntactic suger" and not realy a language entity.




More information about the erlang-questions mailing list