[erlang-questions] Directly matching record fields illegal?

Robert Virding <>
Fri Jan 4 16:30:56 CET 2008


The formal answer is, of course, that Foo#foo.ref is an expression and the
LHS of '=' must be a pattern. '=' is a pattern matching operator, not
assignment. The easiest way to understand this is to view '=' as a shorthand
for a case expression.

What exactly were you trying to do? Set a field in Foo, or test if the ref
fields in Foo and Bar are equal? In the second case it should be '=='
instead.

If it's the first case then you have hit upon the reason why the syntax to
modify a field in a record is the way it is. Trying to come up with a way to
use '=' directly just does not work without changing the meaning of '=' or
have destructive data structures.

So if you would like to change a record field using a syntax like:

    Foo#foo.ref = 5

what would it mean?

- Change the 'ref' field of the 'foo' record in Foo to the value 1, i.e. do
a destructive update.
- Create a new 'foo' record from the one in Foo with the 'ref' field now has
value 1 and put the new record in Foo. I.e. change the value of the variable
Foo.
- Create a new record from the one in Foo and put the value ... . Well
where? I.e. change the meaning of '=' for this special case. You could then
do

    Foo1 = Foo#foo.bar = 1    %%Try explaining this

(Which is close to what we have today: Foo1 = Foo#foo{bar=1})

So all this means that you need a syntax for an expression which takes a
record and returns it with a field which has a new value. Now I agree that
the current syntax might not be very good but I have not seen a better
alternative which is consistent with the rest of language.

By the way, this is exactly the same reason why there isn't a shorter syntax
for accessing a tuple element and using it for setting an element with a
'=', for example:

Tuple{3} and Tuple{3} = new_value

We need a set_element for just this reason. Or perhaps Tuple{3=new_value}.
:-) It would be consistent.

In retrospect it would probably have been better NOT to use '=' as the match
operator but have taken something else. '=' is just too deeply ingrained in
most programmers as an assignment so many things look strange from that
perspective. Just read the beginner's tutorial bloggs when they try to
explain this.

Yes, it has ALWAYS been this way.

Yes, there should probably be more discussion in the documentation about the
implications of non-destructive data structures, variables which aren't and
their implication on syntax.

Robert

On 31/12/2007, Vance Shipley <> wrote:
>
> This suprised me, has it always been this way?
>
>      5> Foo#foo.ref = Bar#bar.ref.
>      ** 1: illegal pattern **
>
>      6> F = Foo#foo.ref.
>      1
>      7> B = Bar#bar.ref.
>      1
>      8> F = B.
>      1
>
> --
>         -Vance
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20080104/a5d0ee60/attachment.html>


More information about the erlang-questions mailing list