Matching elements of records

Ulf Wiger (AL/EAB) ulf.wiger@REDACTED
Thu Oct 28 10:28:04 CEST 2004

On Oct 28 2004, 02:53, Richard A. O'Keefe wrote:
> I note that in the other functional programming languages I use,
> *only* the
> 	<variable> = <pattern>
> 	<variable> @ <pattern>
> 	<variable> as <pattern>
> form is available, not the "reversed" form with the variable 
> on the right.
> I would find the "reversed" form rather confusing and 
> unpleasant to read.

After some digging, I was able to locate a previous post
by Robert Virding, who also has strong opinions on style. ;-)
On this particular issue, the two of you seem to be in 
strong disagreement.

I've pasted in an extract from that post.
To keep Robert from shouting at me, I changed my own coding 
style to match his (on this point). Perhaps it's an acquired 
taste, because I now agree with him that it's better to put
the alias on the right of the expression.




Yes, I know, '=' is used in three contexts:

Explicit match operator
Couple record field name to value/pattern

Aliases in patterns.

The trouble is that there is a limited number of symbols on the 
keyboard so there will be overloading.

>    ugly() ->
>	B = #type{name = 3},
>	fun(A = #type{name = Value}) -> Value end(B).
>    ugly2() ->
>	B = #type{name = 3},
>	case B of
>	    A = #type{name = Value} -> Value
>	end.

I personally can't conceive why anyone would write aliases in patterns 
in that direction, I always do it the other way:

ugly() ->
    B = #type{name = 3},
    fun(#type{name = Value}=A) -> Value end(B).

Writing it this way means you have the same semantics as for a normal 
explicit match, take the value of "A" and match it with the record ...  
This then only gives two uses.

I was much to lenient with the syntax and didn't realise what people
would do with it.  Though after all these years you would think that I
would learn.  It ought to be changed.  So remember:


Hmm, maybe lint should warn? :-)


More information about the erlang-questions mailing list