<div dir="ltr">i would pre<br><br><div class="gmail_quote"><div dir="ltr">On Fri, Dec 4, 2015 at 9:23 AM zxq9 <<a href="mailto:zxq9@zxq9.com">zxq9@zxq9.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Friday 04 December 2015 05:04:23 Benoit Chesneau wrote:<br>
> What is the reasoning of throwing an error on `maps:get/2` instead of<br>
> returning `undefined`? And instead have `maps:find/2`. I am curious. I<br>
> would have expected that get like a proplists would return a default set to<br>
> undefined.<br>
<br>
I've always wondered why these were not universalized across these different modules:<br>
<br>
fetch(Key, Container) -> Value. % or throw exception<br>
<br>
find(Key, Container) -> {ok, Value} | error.<br>
<br>
get(Key, Container) -> get(Key, Value, undefined).<br>
get(Key, Container, Default) -> Value | Default.<br>
<br>
find/2, as defined above could of course be defined in terms of get/3, and the specific names of the functions is a bit arbitrary -- but my point is that a bit more consistency would be nice.<br>
<br>
It is nice to have one that *always* throws an exception on a miss. Its nice to have one that returns a reliable response on a miss. Its nice to have one that always returns a custom default on a miss. Its natural to crash or carry on without having to wrap calls in try..except or make sure that a structure is defined in a way that a stored value will *never* be 'error' or 'undefined' or whatever. All three exist in various APIs, but its one of those weird cases of "A, B, C" <- pick any two.<br>
<br></blockquote><div><br></div><div>I would have expected to crash on a non matching result instead like `{error, notfound}` or `{error, bad_map}` rather than raising an exception. </div><div><br></div><div>IMO, it's a question of convenience there, since throwing an error remove the need to match a result (ie. `Value = maps:get(Maps, Key)` instead of `{ok, Value} = maps:get(Maps, Key)` . I guess I wanted to confirm it or maybe there is another reason for it?<br></div><div><br></div><div>- benoit</div><div><br></div><div><br></div><div><br></div></div></div>