<div dir="ltr"><div><div><div><div>Yes, the variable bitsize variable must be bound. BUT it can
also be bound if it matched "earlier" from inside the same binary. So<br><br></div><span style="font-family:monospace,monospace">b() -> fun (<<N, X:N, _/bitstring>>) -> {N,X} end.</span><br><br></div>and<br><br><span style="font-family:monospace,monospace">1> c("bt").<br>{ok,bt}<br>2> F = bt:b().<br>#Fun<bt.2.81949596><br>3> F(<<8,1,2,3,4>>).<br>{8,1}<br>4> F(<<5,1,2,3,4>>).<br>{5,0}<br></span><br></div>We see that N doesn't have to be imported. So variables in fun argument patterns aren't imported except for the binary bitsize, which is inconsistent and a bit hacky. Why that type?<br><br></div>Robert<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On 30 January 2017 at 00:00, Michel Boaventura <span dir="ltr"><<a href="mailto:michel.boaventura@gmail.com" target="_blank">michel.boaventura@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>According to Joe's Programming Erlang(page 103): Size must be an expression that evaluates to an integer. In pattern matching, Size must be an integer or a bound variable whose value is an integer.</div><div dir="auto"><br></div><div dir="auto">So the N on the pattern match is not a new variable like X.<div><div class="h5"><br><div class="gmail_extra" dir="auto"><br><div class="gmail_quote">Em 29 de jan de 2017 20:42, "José Valim" <<a href="mailto:jose.valim@plataformatec.com.br" target="_blank">jose.valim@plataformatec.com.<wbr>br</a>> escreveu:<br type="attribution"><blockquote class="m_-1902448626598333054quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>From my understanding, the N in this case can never be an unbound variable. If you rename N to M, then it won't compile, so it would not be possible for it to shadow anything.</div><div><br></div><div>It is similar to map keys. They are always treated as values, even when inside patterns.</div><div><br></div><div><br><div class="gmail_quote"><div class="m_-1902448626598333054elided-text"><div>On Sun, Jan 29, 2017 at 23:07 Robert Virding <<a href="mailto:rvirding@gmail.com" target="_blank">rvirding@gmail.com</a>> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="m_-1902448626598333054elided-text"><div class="m_-1902448626598333054m_-504652178806922170gmail_msg"><div class="m_-1902448626598333054m_-504652178806922170gmail_msg"><div class="m_-1902448626598333054m_-504652178806922170gmail_msg"><div class="m_-1902448626598333054m_-504652178806922170gmail_msg"><div class="m_-1902448626598333054m_-504652178806922170gmail_msg">When defining a fun the shadowing of variables occurring in arguments is inconsistent. So with<br class="m_-1902448626598333054m_-504652178806922170gmail_msg"><br class="m_-1902448626598333054m_-504652178806922170gmail_msg"></div><span style="font-family:monospace,monospace" class="m_-1902448626598333054m_-504652178806922170gmail_msg">a(X, N) -><br class="m_-1902448626598333054m_-504652178806922170gmail_msg"></span></div><span style="font-family:monospace,monospace" class="m_-1902448626598333054m_-504652178806922170gmail_msg"> fun (<<X,Y:N,_/binary>>) -> {X,Y} end.</span><br class="m_-1902448626598333054m_-504652178806922170gmail_msg"><br class="m_-1902448626598333054m_-504652178806922170gmail_msg"></div>the X in the fun arguments shadows the X from before it while the N is imported and used. An example:<br class="m_-1902448626598333054m_-504652178806922170gmail_msg"><br class="m_-1902448626598333054m_-504652178806922170gmail_msg"><span style="font-family:monospace,monospace" class="m_-1902448626598333054m_-504652178806922170gmail_msg">1> c(bt).<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">bt.erl:8: Warning: variable 'X' is unused<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">bt.erl:9: Warning: variable 'X' shadowed in 'fun'<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">{ok,bt}<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">2> f(G),G=bt:a(34,8).<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">#Fun<bt.1.1070726><br class="m_-1902448626598333054m_-504652178806922170gmail_msg">3> G(<<1,2,3,4,5>>). <br class="m_-1902448626598333054m_-504652178806922170gmail_msg">{1,2}<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">4> f(G),G=bt:a(34,16).<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">#Fun<bt.1.1070726><br class="m_-1902448626598333054m_-504652178806922170gmail_msg">5> G(<<1,2,3,4,5>>). <br class="m_-1902448626598333054m_-504652178806922170gmail_msg">{1,515}</span><br class="m_-1902448626598333054m_-504652178806922170gmail_msg"><br class="m_-1902448626598333054m_-504652178806922170gmail_msg">Why the difference? Shouldn't we be consistent in shadowing all variables?<br class="m_-1902448626598333054m_-504652178806922170gmail_msg"><br class="m_-1902448626598333054m_-504652178806922170gmail_msg"></div>Robert<br class="m_-1902448626598333054m_-504652178806922170gmail_msg"><br class="m_-1902448626598333054m_-504652178806922170gmail_msg"></div></div>
______________________________<wbr>_________________<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">
erlang-questions mailing list<br class="m_-1902448626598333054m_-504652178806922170gmail_msg">
<a href="mailto:erlang-questions@erlang.org" class="m_-1902448626598333054m_-504652178806922170gmail_msg" target="_blank">erlang-questions@erlang.org</a><br class="m_-1902448626598333054m_-504652178806922170gmail_msg">
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" class="m_-1902448626598333054m_-504652178806922170gmail_msg" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><font color="#888888"><br class="m_-1902448626598333054m_-504652178806922170gmail_msg">
</font></blockquote></div></div><font color="#888888"><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr"><div><div><br></div><div><br></div><div><span style="font-size:13px"><div><span style="font-family:arial,sans-serif;font-size:13px;border-collapse:collapse"><b>José Valim</b></span></div><div><span style="font-family:arial,sans-serif;font-size:13px;border-collapse:collapse"><div><span style="font-family:verdana,sans-serif;font-size:x-small"><a href="http://www.plataformatec.com.br/" style="color:rgb(42,93,176)" target="_blank">www.plataformatec.com.br</a></span></div><div><span style="font-family:verdana,sans-serif;font-size:x-small">Skype: jv.ptec</span></div><div><span style="font-family:verdana,sans-serif;font-size:x-small">Founder and Director of R&D</span></div></span></div></span></div></div></div></div>
</font><br>______________________________<wbr>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
<br></blockquote></div><br></div></div></div></div></div>
</blockquote></div><br></div>