<div dir="ltr"><div>Is there any possibility of getting maps:is_key() as a guard so missing keys can be detected more directly?</div><div><br></div><div>Consider this case of filling defaults into a map before the final clause processes it:</div>
<div><br></div><div><br></div><div><div>myfun(M) when not is_key(abc, M) -></div><div>  myfun(M#{abc=>123});</div><div><br></div><div></div><div>myfun(M) when not is_key(def, M) -></div><div>  myfun(M#{def=>456});</div>
<div><br></div><div></div><div>myfun(M) when not is_key(pqr, M) -><br></div><div><div>  myfun(M#{abc=>789});</div><div><br></div></div></div><div><div>myfun(#{abc:=ABC, def:=DEF, pqr:=PQR}) -></div><div>  do_stuff.</div>
<div><br></div></div><div><br></div><div>I can't see how fail clauses could scale up to something like this.  Now there are certainly other ways of solving the above problem (lists:foldl comes to mind), but I still think having is_key() as a guard would be valuable in certain situations.  It feels about as useful as...<br>
</div><div><br></div><div>f(X) when X/=123 ->  do_stuff.</div><div><br></div><div>... which I don't do often, but it is good to know it is there when I need it.</div><div><br></div><div><br></div><div>Dan.</div><div>
PS.  I accidentally posted the original question to erlang-patches, moving to erlang-questions.<br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Feb 18, 2014 at 2:23 PM, Björn-Egil Dahlberg <span dir="ltr"><<a href="mailto:wallentin.dahlberg@gmail.com" target="_blank">wallentin.dahlberg@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr">Yes, with fail clauses.<div><br></div><div>
f(#{ a := _ }) -> {a,present};</div><div><div>f(_) -> {a,absent}.</div></div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">2014-02-18 20:55 GMT+01:00 Daniel Goertzen <span dir="ltr"><<a href="mailto:daniel.goertzen@gmail.com" target="_blank">daniel.goertzen@gmail.com</a>></span>:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div dir="ltr">Is there a way to pattern-match the absence of a key in a map?<div>
<br></div><div>Dan.<br></div></div>
<br></div>_______________________________________________<br>
erlang-patches mailing list<br>
<a href="mailto:erlang-patches@erlang.org" target="_blank">erlang-patches@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-patches" target="_blank">http://erlang.org/mailman/listinfo/erlang-patches</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div></div>