<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt"><div id="yiv3611998457"><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_10"><span><br clear="none"></span></div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_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="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">However, note that that code also is problematic in another way. </div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12"
style="display:block;"><br></div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_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 style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Value = </div></div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12"
style="display:block;">
case orddict:find(foo, Dict) of</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> {ok, Value} -> Value;</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> error -> Value = calculate_very_expensive_default_value()</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande,
sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> end</div></div></div></blockquote><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_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></div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br></div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_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="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br clear="none"></div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_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 style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">case orddict:find(foo,
Dict) of</div></div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> {ok, Value} -> Value</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> ...</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div
class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">end,</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">... %% use Value afterwards</div></div></div></blockquote><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_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 style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Value =</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> case orddict:find(foo, Dict) of</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;"
class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> {ok, Val} -> Val; %% fresh variable</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> ...</div></div></div><div><div style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> end,</div></div></div><div><div
style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:10pt;" class="yui_3_13_0_ym1_1_1394015531115_10611"><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_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="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br clear="none"></div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_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="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"><br></div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Best,</div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;">Thomas</div><div class="yiv3611998457yahoo_quoted" id="yiv3611998457yui_3_13_0_ym1_7_1394015531115_12" style="display:block;"> <br clear="none"> <div class="yiv3611998457yui_3_13_0_ym1_1_1394015531115_5522" id="yiv3611998457yui_3_13_0_ym1_1_1394015531115_5606" style="font-family: HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif; font-size: 10pt;"> <div class="yiv3611998457yui_3_13_0_ym1_1_1394015531115_5523" id="yiv3611998457yui_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="yiv3611998457yui_3_13_0_ym1_1_1394015531115_5669"> <font id="yiv3611998457yui_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:<div class="yiv3611998457yqt0983349884" id="yiv3611998457yqtfd12765"><br clear="none"> </div></font><div class="yiv3611998457yqt0983349884" id="yiv3611998457yqtfd12399"> </div></div><div class="yiv3611998457yqt0983349884" id="yiv3611998457yqtfd23200"> <blockquote id="yiv3611998457yui_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="yiv3611998457y_msg_container" id="yiv3611998457yui_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 class="yiv3611998457yqt0983349884" id="yiv3611998457yqtfd39393"> </div></div><div class="yiv3611998457yqt0983349884" id="yiv3611998457yqtfd22887"> </div></div><div class="yiv3611998457yqt0983349884" id="yiv3611998457yqtfd89732"> </div></div></div></div></body></html>