octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: thoughts about unwind_protect


From: Jaroslav Hajek
Subject: Re: thoughts about unwind_protect
Date: Tue, 5 Jan 2010 14:21:47 +0100

On Tue, Jan 5, 2010 at 4:52 AM, Robert T. Short
<address@hidden> wrote:
> Sounds more like an argument against global variables than an argument for
> unwind_protect.
>
> I suppose I should use this feature a bit just to understand.  Probably
> won't though.
>
> Bob
>

Not really - sometimes global variables *are* the best solution. Apart
from your own, there is a number of global states in Octave. A typical
example is temporarily disabling the pager:

saved_pager = page_screen_output (false);
unwind_protect
  # do stuff with screen
unwind_protect_cleanup
  page_screen_output (saved_pager);
end_unwind_protect

Other examples may include temporarily disabling a warning,
adding/removing path, or setting system environment variables for
subsequent calls to system(). For extreme examples, look at parcellfun
from the general package :) (and I still think there may be a couple
unwind_protects missing).

In all these cases, unwind_protect gives you a safe and convenient
cleanup solution. try/catch not only can't catch Ctrl-C, it also
requires you to specify the cleanup action twice; once for normal path
and once for exception (unless you catch the error and don't propagate
it).

Note that Matlab also recently introduced another mechanism for
cleanups, the onCleanup object. This will handle Ctrl-C as well; but
unfortunately it has many other drawbacks - unclear order of multiple
cleanups, difficult nesting and block-local cleanups. unwind_protect
is clearly superior, just slightly less convenient to write.

Btw., with modern editors, this is seldom a problem; In my customized
ViM, I just type uwp` and it expands to a complete unwind_protect
block.

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]