[erlang-questions] Illegal Guard?
Fri Feb 5 14:09:15 CET 2016
On 2016年2月5日 金曜日 18:10:33 Theepan wrote:
> Team - Any idea?
> 60> is_integer(A = 5).
> 61> fun () when is_integer(A = 5) -> ok end.
> * 2: illegal guard expression
> 62> fun () when is_integer(5) -> ok end.
> 63> fun (A = 5) when is_integer(A) -> ok end.
I think it has to do with binding VS masking. That is to say, what part
of a function head is not yet referring to an external scope, so you can't
yet create a closure over an existing value:
1> is_integer(A = 5).
2> B = fun() -> A end.
3> C = fun(A = 6) -> A end.
4> D = fun(A) -> A end.
A is now actually assigned to 5 from our first statement -- which is
running within the context of an ongoing fun (I think). That being as
it may, anything assigned within the head of a function, apparently to
include guards, is masking whatever else existed. Its not about
is_integer/1 as much as it is about where it is used.
Now let's drop the current scope's memory of A:
* 1: variable 'A' is unbound
** exception error: no function clause matching erl_eval:'-inside-an-interpreted-fun-'(7)
Now let's see what happens if we use an assignment within a guard within
a function's inner scope:
14> E = fun(A) when is_integer(A) ->
14> case is_integer(Z = A) of
14> true -> Z;
14> false -> "strangeness"
I don't know for sure, but this certainly leads me to believe it has
something to do with where the actual scoping boundary lies.
In other news... another thought crossed my mind...
WHY ARE YOU ASSIGNING INSIDE A GUARD?
Just to deepen the mystery because needlessly exploring corner cases
that should never occur in actual code is sort of funny to me...
16> A = 5.
17> F = fun(A) when is_integer(A) ->
17> case is_integer(Z = A) of
17> true -> Z;
17> false -> "strangeness"
Someone who really knows will get bored and give a real answer on Monday,
More information about the erlang-questions