Strange behaviour with a record in a guard
    Åke Johansson AI (EAB) 
    ake.ai.johansson@REDACTED
       
    Mon Mar  3 10:07:28 CET 2003
    
    
  
...
-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?
A sample program and sample run is found below.
/ Ake
-module(mt).
-export([run/0]).
-record(r1, {f1}).
run() ->
    Tuple = {undefined,undefined, undefined},
    io:format("Tuple = ~w~n", [Tuple]),
    Tuple2 = replace_undefined_f1_in_record_r1(Tuple),
    io:format("Tuple2 = ~w~n", [Tuple2]).
replace_undefined_f1_in_record_r1(R) when R#r1.f1 == undefined ->
    io:format("replace_undefined_f1_in_record_r1: R=~w~n", [R]),
    R#r1{f1 = replaced};
replace_undefined_f1_in_record_r1(AnythingElse) ->
    AnythingElse.
Erlang (BEAM) emulator version 5.1.1 [threads:0]
Eshell V5.1.1  (abort with ^G)
1> 
=PROGRESS REPORT==== 3-Mar-2003::09:10:55 ===
          supervisor: {local,sasl_safe_sup}
...
                       {shutdown,2000},
                       {child_type,worker}]
=PROGRESS REPORT==== 3-Mar-2003::09:10:55 ===
         application: sasl
          started_at: nonode@REDACTED
1> c(mt).
{ok,mt}
2> mt:run().
Tuple = {undefined,undefined,undefined}
replace_undefined_f1_in_record_r1: R={undefined,undefined,undefined}
Tuple2 = {r1,replaced}
ok
3> 
    
    
More information about the erlang-questions
mailing list