clarification on single assignment

Hakan Mattsson hakan@REDACTED
Fri Nov 24 13:22:34 CET 2000


On Fri, 24 Nov 2000, Robert Virding wrote:

rv> DON'T confuse the '=' in records with the match operator, all it does 
rv> is couple a field name with a value/pattern.  You could use any 
rv> symbol there, for example B#type{name |-| 3} or, if you are really 
rv> perverse, B#type{name should be coupled to 3}.

You mean as perverse as the spurious ';' separator? ;-)

The semantics of the '=' operator depends of its context, but the '.'
could not be reused as separator between function clauses!?

rv> So this means that in your last example what you are trying to do is:
rv> 
rv> B = #type{name = 3},
rv>	    Create a record of type #type where field 'name' has value 3
rv>	    and bind B to the record.
rv> A = B#type{name = Value},
rv>	    Create a copy of the value of B where field 'name' now has the
rv>	    value of the unbound variable Value.
rv> Value
rv>	    Return the value of the unbound variable Value.
rv> 
rv> Which, of course, according to the rules does not work.  The first 
rv> example, of course, works because you bind the variables before you use 
rv> them.

Perverse or not, I think that the different semantics of the '='
operator in its various contexts (match/assign), is a little bit
confusing. The bad example below does not work as you already pointed
out, but both the ugly ones does. It is not intuitive.

    -module(equal).
    -compile(export_all).
    -record(type, {name}).
    
    good() ->
	Value = 3,
	B = #type{},
	A = B#type{name = Value}.
    
    %% bad() ->
    %%     B = #type{name = 3},
    %%     A = B#type{name = Value},
    %%     Value.
    
    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.

/Håkan





More information about the erlang-questions mailing list