What atom name to represent a null value

Tim Fletcher <>
Mon Mar 1 11:18:39 CET 2010


Joe Armstrong wrote:

> The best way to express this in Erlang is as follows:
>
> foo(1) -> 2;
> foo(2) -> 3;
> foo(3) -> 1;
> foo(X) -> exit({ebadArgToFoo, X}).

Surely "best" or "correct" depends on the context? On reflection it
isn't a very good example, because as you point out you'd usually want
a function that returns integers to only return integers. I don't
think that invalidates the idea though.


Joe Armstrong wrote:

> Error handling in Erlang is supposed to be "not defensive"
> you should only write code for the expected cases.

Sure, so if being undefined is an expected case then it isn't being
defensive to return the atom 'undefined', regardless of the type of
the function?


Robert Virding wrote:

> If, on the other hand, this is not an error but just the default case
> where you can return something sensible then adding the extra clause
> makes sense.
>
> As an example of the difference: dict:find/2 returns (the atom)
> 'error' if a key is found, while dict:fetch/2 throws an error if the
> key is not found as it is defined to assume the key does exist.

Yep, this is a better example. I don't think 'error' is the best atom
to use as it might not necessarily be an error for the key not to
exist. Again, it depends on the context.


Tim


More information about the erlang-questions mailing list