Strange behaviour with a record in a guard

Bengt Kleberg eleberg@REDACTED
Mon Mar 3 10:19:12 CET 2003


> From: Åke Johansson AI (EAB) <ake.ai.johansson@REDACTED>
> To: erlang-questions@REDACTED
> Subject: Strange behaviour with a record in a guard
> Date: Mon, 3 Mar 2003 10:07:28 +0100 
> MIME-Version: 1.0
> 
> ...
> -record(r1,{f1})
> 
> foo(R) when R#r1.f1 == undefined ->
> 	ok.
> ...	
> 
> It seems like foo(R) matches any tuple of size 2 or bigger, where the second 
element is undefined. Should not the compiler add code which checks that R is a 
record of the correct type?


R#r1.f1 only gets the ''right'' element from the tuple.

if you want to check the record type, as well as the value of a field,
you need to write (here ''= R'' may be omitted if you are not
interested in the whole record):

foo( #r{f1=F1} = R ) when F1 == undefined ->
	ok.


note that this means it is usually better to do

#r{f1=F1} = R,

than

F1 = R#r.f1,


bengt




More information about the erlang-questions mailing list