Record comprehension

Ulf Wiger <>
Thu Jul 13 08:30:49 CEST 2000

On Wed, 12 Jul 2000, Craig Dickson wrote:

>Well, that would be because a record is really just a tuple with the name of
>the record type as the first member. Thus
>#tester{first=first,second=second,third=third} is really just
>{tester,first,second,third}. Though I'm a little surprised that the lookup
>of H#tester.second didn't cause a runtime error due to H not being the right
>kind of record.

Yes, it is a bit surprising.
I believe the main reason is that testing whether a variable is
actually of the proper record type requires type analysis, which is
something the current compiler doesn't do. For all other types, the
runtime system will perform basic type checks, but since there is no
record type, the runtime system can't do much.

If you compile test.erl with the 'E' option:

test() ->
    H = {first,second,third},
    R = {tester,first,second,third},

module_info() ->

module_info(_) ->

you'll see that the pre-processor doesn't give the VM a chance to
reason about the records. It could insert Erlang code to verify that:
- H is a tuple of the correct size
- element(1, H) == tester


test() ->
    H = {first, second, third},
    R = #tester{first = first, second = second, third = third},
    {if size(H) == 4, element(1,H) == tester ->
             element(3, H);
	true ->
             exit({invalid_record, H})
     if size(R) == 4, element(1, R) == tester ->
	     element(3, R);
        true ->
             exit({invalid_record, R}

but this would cause significant overhead, as the record selector
syntax is one of the most common operations in many typical systems.

There are some solutions in the (near?) future:

- The tagging scheme has been changed in OTP R7B (due Sept)
  While there isn't a special record type in R7B, it will be more 
  feasible to create one.

- The upcoming type checker does catch this error, assuming that you
  have made the proper type annotations (it doesn't derive types.)


>----- Original Message -----
>From: "Martin Logan" <>
>To: <>
>Sent: Wednesday, 12 July 2000 02:36 pm
>Subject: Record comprehension
>> I seem to have encountered somthing interesting with records. If anyone
>> has any insight please share.
>> -module(test).
>> -export([test/0]).
>> -record(tester, {first, second, third}).
>> test() ->
>>     H = {first, second, third},
>>     R = #tester{first=first, second=second, third=third},
>>     {H#tester.second, R#tester.second}.
>> 32> c(test).
>> {ok,test}
>> 33> test:test().
>> {third,second}
>> Thanks,
>> Martin Logan

Ulf Wiger                                    tfn: +46  8 719 81 95
Network Architecture & Product Strategies    mob: +46 70 519 81 95
Ericsson Telecom AB,              Datacom Networks and IP Services
Varuvägen 9, Älvsjö,                    S-126 25 Stockholm, Sweden

More information about the erlang-questions mailing list