[erlang-questions] Comparing (pattern-matching) records

Adrain Roe roe.adrian@REDACTED
Sat Mar 31 09:48:23 CEST 2012


7) is saying "I'd like a rec record that matches a rec record where a is 1. It is a pattern match rather than an assertion that the lhs (with defaults for all unspecified fields) == rhs (with a =1 and b with its default value). 

I too found this somewhat confusing to start with, but this pattern matching is precisely where much of erlang's remarkable brevity and readability stems from. 

Adrian 

Sent from my iPhone

On 31 Mar 2012, at 08:37, "Avinash Dhumane" <avinash@REDACTED> wrote:

> I might have overlooked all the 3 books (for sections on records), but couldn't find an explanation why a pattern matching error doesn't occur in the last (7th) shell expression below.
> 
> Please.
> 
> Thanks.
> 
> $ cat rec.hrl
> -record(rec, {a, b}).
> $ erl
> Erlang R14B02 (erts-5.8.3) [smp:2:2] [rq:2] [async-threads:0]
> 
> Eshell V5.8.3  (abort with ^G)
> 1> rr("rec.hrl").
> [rec]
> 2> A=#rec{}.
> #rec{a = undefined,b = undefined}
> 3> B=#rec{a=1}.
> #rec{a = 1,b = undefined}
> 4> A=B.
> ** exception error: no match of right hand side value #rec{a = 1,b = undefined}
> 5> {} = {1}.
> ** exception error: no match of right hand side value {1}
> 6> {rec, undefined, undefined} = {rec, 1, undefined}.
> ** exception error: no match of right hand side value #rec{a = 1,b = undefined}
> 7> #rec{} = #rec{a=1}.
> #rec{a = 1,b = undefined}
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list