<div dir="ltr"><span style="font-size:13px">"WHY ARE YOU ASSIGNING INSIDE A GUARD?"</span><br style="font-size:13px"><div><span style="font-size:13px"><br></span></div><div>There was another thread regarding the issues about automatic masking of binaries during construction. I was checking to see how to convert the lvalue into a binary (so that it will get masked as well.) before matching takes place.. Sometimes I try some crazy things :)</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 5, 2016 at 6:39 PM, zxq9 <span dir="ltr"><<a href="mailto:zxq9@zxq9.com" target="_blank">zxq9@zxq9.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 2016年2月5日 金曜日 18:10:33 Theepan wrote:<br>
> Team - Any idea?<br>
><br>
</span><span class="">> 60> is_integer(A = 5).<br>
> true<br>
> 61> fun () when is_integer(A = 5) -> ok end.<br>
> * 2: illegal guard expression<br>
</span><span class="">> 62> fun () when is_integer(5) -> ok end.<br>
> #Fun<erl_eval.<a href="tel:20.54118792" value="+12054118792">20.54118792</a>><br>
</span><span class="">> 63> fun (A = 5) when is_integer(A) -> ok end.<br>
> #Fun<erl_eval.6.54118792><br>
<br>
</span>I think it has to do with binding VS masking. That is to say, what part<br>
of a function head is not yet referring to an external scope, so you can't<br>
yet create a closure over an existing value:<br>
<br>
1> is_integer(A = 5).<br>
true<br>
2> B = fun() -> A end.<br>
#Fun<erl_eval.<a href="tel:20.54118792" value="+12054118792">20.54118792</a>><br>
3> C = fun(A = 6) -> A end.<br>
#Fun<erl_eval.6.54118792><br>
4> D = fun(A) -> A end.<br>
#Fun<erl_eval.6.54118792><br>
<br>
A is now actually assigned to 5 from our first statement -- which is<br>
running within the context of an ongoing fun (I think). That being as<br>
it may, anything assigned within the head of a function, apparently to<br>
include guards, is masking whatever else existed. Its not about<br>
is_integer/1 as much as it is about where it is used.<br>
<br>
5> A.<br>
5<br>
6> B().<br>
5<br>
7> C(6).<br>
6<br>
8> D(7).<br>
7<br>
<br>
Now let's drop the current scope's memory of A:<br>
<br>
9> f(A).<br>
ok<br>
10> A.<br>
* 1: variable 'A' is unbound<br>
11> B().<br>
5<br>
12> C(7).<br>
** exception error: no function clause matching erl_eval:'-inside-an-interpreted-fun-'(7)<br>
13> D(7).<br>
7<br>
<br>
Now let's see what happens if we use an assignment within a guard within<br>
a function's inner scope:<br>
<br>
14> E = fun(A) when is_integer(A) -><br>
14> case is_integer(Z = A) of<br>
14> true -> Z;<br>
14> false -> "strangeness"<br>
14> end<br>
14> end.<br>
#Fun<erl_eval.6.54118792><br>
15> E(10).<br>
10<br>
<br>
I don't know for sure, but this certainly leads me to believe it has<br>
something to do with where the actual scoping boundary lies.<br>
<br>
In other news... another thought crossed my mind...<br>
<br>
WHY ARE YOU ASSIGNING INSIDE A GUARD?<br>
<br>
Just to deepen the mystery because needlessly exploring corner cases<br>
that should never occur in actual code is sort of funny to me...<br>
<br>
16> A = 5.<br>
5<br>
17> F = fun(A) when is_integer(A) -><br>
17> case is_integer(Z = A) of<br>
17> true -> Z;<br>
17> false -> "strangeness"<br>
17> end<br>
17> end.<br>
#Fun<erl_eval.6.54118792><br>
18> F(5).<br>
5<br>
19> F(10).<br>
10<br>
20> A.<br>
5<br>
<br>
Someone who really knows will get bored and give a real answer on Monday,<br>
perhaps... ?<br>
<br>
-Craig<br>
<div class="HOEnZb"><div class="h5">_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</div></div></blockquote></div><br></div>