[erlang-questions] How do I elegantly check many conditions?
mats cronqvist
masse@REDACTED
Tue Mar 24 21:24:03 CET 2009
Hynek Vychodil <vychodil.hynek@REDACTED> writes:
> code
>
> ok = is_new_email1(Email).
>
> doesn't looks like same amount of code
>
> try is_new_email2(Email) catch throw:email_in_use -> email_in_use end.
>
> First one turns return value to exception, second one turns exception to
> return value. Second one seems more code, does not?
but the exception generated in example 1 is a worthless 'badmatch'.
> Even code
>
> is_new_email1(Email) ->
> case mnesia:dirty_read(email, Email) of
> [] -> ok;
> _ -> email_in_use
> end.
>
> seems less than
>
> is_new_email2(Email) ->
> case mnesia:dirty_read(email, Email) of
> [] -> ok;
> _ -> throw(email_in_use)
> end.
I think it should be written like this;
is_new_email2(Email) ->
try [] = mnesia:dirty_read(email, Email)
catch _:_ -> throw({email_in_use,Email})
end.
This is how your example 1 above has to be written too, if it is to
give a helpful error message.
mats
More information about the erlang-questions
mailing list