Record selectors (again)

Jay Nelson jay@REDACTED
Thu Jun 24 04:56:56 CEST 2004

 > Kostis Sagonas wrote:
 >  -module(rec).
 >  -export([unsafely_access_a/1]).
 >  -record(rec,{a,b}).
 >  unsafely_access_a(R) ->
 >      R#rec.a.

 > Vance Shipley replied:
 > It seems to me that it's up to you to test if the argument is
 > correct or not.  Otherwise it is handled through exception handling
 > (i.e. you may want it to crash).

 > unsafely_access_a(R) when is_record(R, rec) ->
 >       R#rec.a.

Using the current compiler, the following
shorthand way provides the same safety (and is the way I
code all my record arguments now):

unsafely_access_a(#rec{a=Value}) ->

The pattern matcher checks the record type before allowing
a successful match.  The syntax for the more equivalent
approach is:

unsafely_access_a(#rec{} = R) ->


More information about the erlang-questions mailing list