[erlang-questions] maps API

zxq9 zxq9@REDACTED
Fri Dec 4 09:22:47 CET 2015


On Friday 04 December 2015 05:04:23 Benoit Chesneau wrote:
> What is the reasoning of throwing an error on `maps:get/2` instead of
> returning `undefined`? And instead have `maps:find/2`.  I am curious. I
> would have expected that get like a proplists would return a default set to
> undefined.

I've always wondered why these were not universalized across these different modules:

    fetch(Key, Container) -> Value. % or throw exception

    find(Key, Container) -> {ok, Value} | error.

    get(Key, Container) -> get(Key, Value, undefined).
    get(Key, Container, Default) -> Value | Default.

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.

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.

Since its Christmas, I would also ask Santa for a partition/2 that was common to all containers-of-lists type thingies.

-Craig



More information about the erlang-questions mailing list