Avoid case nesting

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Avoid case nesting

Vladimir Sekissov
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
end.

Is there better and more elegant approaches?

Best Regards,

Vladimir Sekissov


Reply | Threaded
Open this post in threaded view
|

Avoid case nesting

Thomas Lindgren-3

All I can think of is wrapping things up a bit more.

case catch begin ... end of
   {ok, Res} -> Res;
   Err -> handle_error(Err)
end

return_error({'EXIT', ...}) -> %% and so on

There is a common further difficulty, however: when you have a
state that needs to be processed if there is an error,
you also must ensure that the state is thrown with the
rest of the error. (For the example above, if you want to log the current
state
record at an error inside the begin ... end, handle_error
has to get that state record somehow.)

The obvious solution to this "state visibility problem" is
to write the log where the error occurs, which naturally
leads to nested cases. Perhaps someone has a better solution?

-- Thomas

-----Original Message-----
From: owner-erlang-questions
[mailto:owner-erlang-questions]On Behalf Of Vladimir Sekissov
Sent: den 13 januari 2002 19:00
To: erlang-questions
Subject: Avoid case nesting


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
end.

Is there better and more elegant approaches?

Best Regards,

Vladimir Sekissov



Reply | Threaded
Open this post in threaded view
|

Avoid case nesting

Ulf Wiger-4
In reply to this post by Vladimir Sekissov

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} ->
      freezing;
   {true, true} ->
      fairly_comfy;
   {false, true} ->
      sweating
end.

The compiler will not construct the tuple used in the case
pattern.

/Uffe

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
>end.
>
>Is there better and more elegant approaches?
>
>Best Regards,
>
>Vladimir Sekissov
>