[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: locally changing global variables
From: |
John W. Eaton |
Subject: |
Re: locally changing global variables |
Date: |
Tue, 18 May 2010 02:26:57 -0400 |
On 18-May-2010, Jaroslav Hajek wrote:
| Currently, I think not. It would be possible if we had a working
| onCleanup implementation. Despite we have unwind_protect, which is, in
| most practical ways, more versatile, onCleanup being an object allows
| for doing magic, such as injecting a cleanup action to caller scope
| (which could be the solution here). I'll start a separate thread about
| this.
Thinking about this a little more, I guess I'd like to do something
like:
function retval = my_special_value (val, opt)
persistent curr_val = "default_value";
make_local = nargin == 2 && strcmp (opt, "local");
if (nargin == 1 || nargin == 2)
if (nargin == 2)
__make_local_variable__ (@my_special_value, curr_val);
endif
if (nargout > 0)
retval = curr_val;
endif
curr_val = val;
elseif (nargin == 0)
retval = curr_val;
else
print_usage ();
endif
endfunction
Is there no way to have the __make_local_variable__ function call
above hook into your current mechanism for restoring variable values?
I guess it would need to put a function in the unwind_protect block
for the calling scope that would use the given function handle to
reset the variable to curr_val.
Of course, if you had to repeat this very often, it would also be nice
to be able to hide the logic in a single function, but I don't see a
good way to do that.
jwe