matlab equivalent of unwind_protect in R2008a
David Bateman
David.Bateman at motorola.com
Mon Mar 17 10:58:09 CDT 2008
Seems matlab finally introduce the equivalent of unwind_protect in
R2008a with the onCleanup function. The example they give is like
function changeDirectorySafely(fileName)
currentDir = pwd;
c = onCleanup(@()cd(currentDir));
functionThatMayError;
end % c executes cd(currentDir) here
and they state that the cd(currentDir) is executed even if the function
creates an error. This is implemented in the new object class
definitions, where the destructors for "c" are always called when c is
destroyed. The stupid thing about this is that
function changeDirectorySafely(fileName)
currentDir = pwd;
onCleanup(@()cd(currentDir));
functionThatMayError;
end % c executes cd(currentDir) here
will not do the same as the above as "ans" is destroyed immediately and
so the cleanup code is executed early.. I don't believe there are
destructors in the Octave object classes which are compatible with
Matlab v7.5 and earlier, and so we can't yet do it this way. However,
with a bit of manipulation of the unwind_protect stack we can get the
same behavior.
We have to be careful of cases like
function changeDirectorySafely(fileName, cleanup)
currentDir = pwd;
if (cleanup)
c = onCleanup(@()cd(currentDir));
end
functionThatMayError;
end % c executes cd(currentDir) here
as the cleanup will only happen if the "cleanup" variable is true, and
this is no longer the same structure as a traditional unwind_protect
block. So we can't use directly the unwind_protect code in the parser.
However, something like the attached seems to implement the same
behavior. The only thing it won't do the same is something like
function changeDirectorySafely(fileName)
currentDir = pwd;
c = onCleanup(@()cd(currentDir));
functionThatMayError;
clear c; % c executes cd(currentDir) here
functionThatWontError;
end
as the cleanup occurs when c is cleared. Do we care? Is the attached
function useful? Do you John prefer to implement the classdef code with
the destructors and really do this function in a matlab compatible manner?
Regards
David
--
David Bateman David.Bateman at motorola.com
Motorola Labs - Paris +33 1 69 35 48 04 (Ph)
Parc Les Algorithmes, Commune de St Aubin +33 6 72 01 06 33 (Mob)
91193 Gif-Sur-Yvette FRANCE +33 1 69 35 77 01 (Fax)
The information contained in this communication has been classified as:
[x] General Business Information
[ ] Motorola Internal Use Only
[ ] Motorola Confidential Proprietary
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: patch7711
Url: https://www.cae.wisc.edu/pipermail/octave-maintainers/attachments/20080317/e5481033/attachment-0001.ksh
More information about the Octave-maintainers
mailing list