<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt"><div><span>(The beginning of that mail looks like Yahoo did a remix of multiple drafts, but I think the rest is okay. Sorry.)</span></div><div style="color: rgb(0, 0, 0); font-size: 13px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;"><span><br></span></div><div style="color: rgb(0, 0, 0); font-size: 13px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;"><span>Best,</span></div><div style="color: rgb(0, 0, 0); font-size: 13px; font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; background-color: transparent; font-style: normal;"><span>Thomas</span></div><div
class="yahoo_quoted" style="display: block;"> <br> <br> <div style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"> <div style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 12pt;"> <div dir="ltr"> <font size="2" face="Arial"> On Wednesday, March 5, 2014 12:18 PM, Thomas Lindgren <thomasl_erlang@yahoo.com> wrote:<br> </font> </div> <blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; margin-top: 5px; padding-left: 5px;"> <div class="y_msg_container"><div id="yiv8167445075"><div><div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div id="yiv8167445075"><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255,
255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_10"><span><br clear="none"></span></div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display: block;">However, note that you don't want to write that sort of code anyway!</div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">However, note that that code also is problematic in another way. </div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br clear="none"></div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">What happens if we modify the second clause of your example to make Value safe? </div></div></div></div><blockquote
style="border:none;padding:0px;"><div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Value = </div></div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">
case orddict:find(foo, Dict) of</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> {ok, Value} -> Value;</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> error -> Value = calculate_very_expensive_default_value()</div></div></div><div><div
class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> end</div></div></div></blockquote><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><span style="font-size:10pt;">How does this evaluate? If orddict finds foo, variable Value is bound in {ok, Value} in the first clause of the case. Then that value is returned from the case clause and MATCHED
WITH the binding of the variable Value (itself). Recall that, since Value has been bound in the case clause previously, "Value = case ... end" means matching, not just variable binding. The same reasoning goes
for the second clause. </span><br clear="none"></div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br clear="none"></div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">The basic problem is that the code above mixes two ways of writing erlang. It works, but only by accident, and only at the cost of
needlessly walking Value a number of times matching itself. (See the end of this mail for the underlying problem, though.)</div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br clear="none"></div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Here are the two ways to get it right. First, t<span style="font-size:10pt;">he old way of writing the same code, which these days give warnings about exported variables:</span></div></div></div><blockquote style="border:none;padding:0px;"><div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12"
style="display:block;">case orddict:find(foo,
Dict) of</div></div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> {ok, Value} -> Value</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> ...</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);
font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">end,</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">... %% use Value afterwards</div></div></div></blockquote><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted"
id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Note that there's no enclosing "Value = case ... end" up there. <span style="font-size:10pt;">Then the new way (which is what most of us use
now):</span></div></div></div><blockquote style="border:none;padding:0px;"><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Value =</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> case orddict:find(foo, Dict) of</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color:
rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> {ok, Val} -> Val; %% fresh variable</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> ...</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial,
'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> end,</div></div></div><div><div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_10611" style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">... %% use Value, but not Val</div></div></div></blockquote><div><div style="color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br clear="none"></div><div
class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">In the larger scheme of things: The way I see it, the feature of implicit variable matching is more trouble than it's worth. Warnings about such matches would catch
this kind of problems.</div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br clear="none"></div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Best,</div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Thomas</div><div class="yiv8167445075yahoo_quoted" id="yiv8167445075yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> <br clear="none"> <div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_5522" id="yiv8167445075yui_3_13_0_ym1_1_1394015531115_5606" style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"> <div class="yiv8167445075yui_3_13_0_ym1_1_1394015531115_5523" id="yiv8167445075yui_3_13_0_ym1_1_1394015531115_5605" style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica,
Arial, 'Lucida Grande', sans-serif; font-size: 12pt;"> <div dir="ltr" id="yiv8167445075yui_3_13_0_ym1_1_1394015531115_5669"> <font id="yiv8167445075yui_3_13_0_ym1_1_1394015531115_5668" size="2" face="Arial"> On Tuesday, March 4, 2014 11:41 PM, Szoboszlay Dániel <dszoboszlay@gmail.com> wrote:</font><div class="yiv8167445075yqt4449035871" id="yiv8167445075yqtfd54021"><div class="yiv8167445075yqt0983349884" id="yiv8167445075yqtfd12765"><br clear="none"> </div><div class="yiv8167445075yqt0983349884" id="yiv8167445075yqtfd12399"> </div></div></div><div class="yiv8167445075yqt4449035871" id="yiv8167445075yqtfd95441"><div class="yiv8167445075yqt0983349884" id="yiv8167445075yqtfd23200"> <blockquote id="yiv8167445075yui_3_13_0_ym1_1_1394015531115_5604" style="border-left-width:2px;border-left-style:solid;border-left-color:rgb(16, 16, 255);padding-left:5px;"> <div class="yiv8167445075y_msg_container"
id="yiv8167445075yui_3_13_0_ym1_1_1394015531115_5603"><br clear="none">Some examples where I really miss them:<br clear="none"><br clear="none">foo(Dict) -><br clear="none"> Value = case
orddict:find(foo, Dict) of<br clear="none"> {ok, Value} -> Value;<br clear="none"> error -> calculate_very_expensive_default_value()<br clear="none"> end,<br clear="none"> ...<br clear="none"><br clear="none">Sorry, variable 'Value' is
unsafe in 'case', please rename your internal <br clear="none">variable to 'Value1' or refactor this humble four-liner into a <br clear="none">get_foo_from_orddict/1 function, whichever you feel more inconvenient.<br clear="none"><br clear="none"></div> </blockquote> </div></div></div><div class="yiv8167445075yqt4449035871" id="yiv8167445075yqtfd71952"><div class="yiv8167445075yqt0983349884" id="yiv8167445075yqtfd39393"> </div></div></div><div class="yiv8167445075yqt4449035871" id="yiv8167445075yqtfd44609"><div class="yiv8167445075yqt0983349884" id="yiv8167445075yqtfd22887"> </div></div></div><div class="yiv8167445075yqt4449035871" id="yiv8167445075yqtfd36446"><div class="yiv8167445075yqt0983349884" id="yiv8167445075yqtfd89732"> </div></div></div></div></div></div></div><br><br></div> </blockquote> </div> </div> </div> </div></body></html>