Avoid case nesting

Ulf Wiger <>
Mon Jan 14 18:12:55 CET 2002

I've done this type of thing myself, and think it's a good
solution esp. for tools, where you want a top-level catch and
"pretty-print" error messages.

If all you want to do is nest case statements, you can do
something like this:

case {is_cold_outside(), have_warm_clothes_on()} of
   {true, false} ->
   {true, true} ->
   {false, true} ->

The compiler will not construct the tuple used in the case


On Sun, 13 Jan 2002, Vladimir Sekissov wrote:

>Good day,
>To avoid many nested cases I use following simple constract:
>case catch
>    begin
>        {ok, MgrPid} = start_link(), %if wrong we'll get badmatch here
>        ...
>        {ok, GoodResult}
>    end of
>    {'EXIT', {{badmatch, {error, Error}}, _}} ->
>        {error, Error};
>    {'EXIT', {{badmatch, Error}, _}} ->
>        {error, Error};
>    {'EXIT', Error} ->
>            {error, Error};
>    {ok, Result} ->
>        Result
>Is there better and more elegant approaches?
>Best Regards,
>Vladimir Sekissov

More information about the erlang-questions mailing list