On 24/04/2012, at 10:52 AM, Robert Virding wrote:
> I have missed one part of the discussion here and that is about ?=:
> why use ?= in a guard and not =, and what does ?= mean outside a
> guard?
Pat = Expr
in an expression has the value and effect of
case Expr of X = Pat -> X end.
Combine this with the fact that in a guard, an expression is now
allowed as a guard test.
bar(X) when X -> 1;
bar(X) -> 2.
2> foo:bar(true).
1.
3> foo:bar(false).
2.
4> foo:bar([]).
2.
The problem now is that EITHER
f(X) when X = [] -> ...
> f([])
fails (because the match X = [] succeeds but has value [] which is
taken as false in a guard) OR the behaviour of = in a guard is
inconsistent with its behaviour in an expression.
THEREFORE
Pat ?= Expr
is defined to have the value of
case Expr of Pat -> true ; _ -> false end
(er, this is just what it says about the _value_, ok?)
and to have this value in ALL contexts, both guards and expressions.
>
> I think if we could avoid introducing a new operator just for this
> would be good.
We could have. But once 'and' 'or' 'andalso' 'orelse' and so on were
allowed in guards, it was too late. As far as I can see, we have just
three choices:
(0) the status quo
(1) introduce a new operator
(2) give Pat = Expr the value of Expr in an expression
but true/false in a guard, which means that there
will be expressions that are legal in both guard and
expression contexts and will succeed quietly in both
but with different values.