I’m attempting to use a record in a guard as described here[1]. If I use the short form described there:
handle(Msg, State) when Msg==#msg{to=void, no=3} ->
… I never get a match… however, if I fully expand it to:
handle(Msg, State) when Msg#msg.to==void, Msg#msg.no==3 ->
… all is well. As it seems I do with most erlang docs, am I reading it wrong?
Thanks,
–tim
[1] – http://www1.erlang.org/doc/reference_manual/records.html#id2278275
When you say
#msg{to=void, no=3}in a guard, all the fields that you haven’t mentioned will be set to their default (usuallyundefined). So your guard is failing to match because some of the fields not listed don’t match.I tend to always use a pattern rather than a guard where possible, so I would write the clause as:
This pattern requires Msg to msg record (a tuple the size of an msg record with the first element being
msg), thetoelement must bevoidand thenoelement must be 3. The other elements of the msg record can be anything.