[erlang-questions] Erlang elseif

Robert Virding rvirding@REDACTED
Thu Nov 20 14:16:34 CET 2008


In one respect this is not too bad. We have always viewed throw/catch not as
an exception but as a non-local return, so you are not really generating an
exception. This is an unfortunate side effect of having a try which catches
everything, catch has the same problem. I *know* that you can choose what to
catch in try, but it does place it at the same "level" as exceptions.

This is perhaps a suggestion for Erlang3000, try for exits/exceptions and
throw/catch as a matching pair where catch only catches throws.

And one extra suggestion I can't help but make is to use LFE and do:

(cond ((: lists member a caps) (: io format '"Member of capitals\n"))
     ((: lists member a small) (: io format '"Member of smalls\n"))
     ((: lists member a nums) (: io format '"Member of nums\n"))
     (else (: io format '"Member of capitals\n")))

:-)

Robert


2008/11/20 <kdronnqvist@REDACTED>

> Thanks for your solutions, they give me good insight in how the Erlang
> world would solve this. I have one more way of solving this that I think is
> kind of clean and it would be efficient because the lists:member() would
> need to be evaluated once a match is found.
>
> elseif5(A) ->
>     try
>         lists:member(A,?CAPS) andalso throw(caps),
>         lists:member(A,?SMALL) andalso throw(small),
>         lists:member(A,?NUMS) andalso throw(nums),
>         throw(none)
>     catch
>         throw:caps -> io:format("Member of capital\n");
>         throw:small -> io:format("Member of small\n");
>         throw:nums -> io:format("Member of nums\n");
>         throw:none -> io:format("Not a member\n");
>         X:Y -> io:format("Bad exception ~p:~p\n",[X,Y])
>     end.
>
> However, some might think this is the wrong way of handling exceptions.
> What do you think?
>
> BR,
> Daniel Rönnqvist
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20081120/bebde25f/attachment.htm>


More information about the erlang-questions mailing list