Record selectors (again)

Kostis Sagonas kostis@REDACTED
Tue Jun 22 18:27:21 CEST 2004


This is a plea for a small change to the compilation of the
record selector operations in BEAM.  Apologies for opening
up an old thread, but I cannot see any good excuses for
maintaining the following behaviour...

The code:
==========================================================
-module(rec).
-export([unsafely_access_a/1]).
-record(rec,{a,b}).

unsafely_access_a(R) ->
    R#rec.a.
==========================================================

currently gets preprocessed to:
==========================================================
unsafely_access_a(R) ->
    erlang:element(2,R).
==========================================================

which loses all information that the `a' field of an `rec'
record is requested. This is VERY user-unfriendly and REALLY
error-prone.  For example, the following happily returns:

1> rec:unsafely_access_a({gazonk,[total_crap],bar,42}).


Modulo returning different exceptions and a slight performance
hit (which should not be prohibitive or a reason for not doing
this change in OTP), it seems to me that the following code
follows the intention of the programmer much more closely:

==========================================================
unsafely_access_a(R) ->
    {rec,A,_} = R,
    A.
==========================================================

Perhaps a better translation than the one I suggest is possible,
but clearly something needs to be done about this Erlang-ism...

Kostis.




More information about the erlang-questions mailing list