octave-maintainers
[Top][All Lists]
Advanced

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

Re: locally changing global variables


From: Jaroslav Hajek
Subject: Re: locally changing global variables
Date: Tue, 11 May 2010 08:08:01 +0200

On Mon, May 10, 2010 at 9:07 PM, Søren Hauberg <address@hidden> wrote:
> man, 10 05 2010 kl. 13:48 +0200, skrev Jaroslav Hajek:
>> Octave's global pseudo-variables are a useful tool for customizing
>> Octave's settings, yet they present problems when a function is
>> dependent on a particular state of a certain variable. A typical
>> instance in my work is console output magic used for progress tracking
>> of a lengthy task that may be delayed or even completely spoiled by
>> the pager. A temporary change of a particular variable can be achieved
>> by unwind_protect:
>
> [snip]
>
>> Comments? Suggestions?
>
> I agree with your observation that the current syntax is a bit more
> verbose than you'd really like it to be. The syntax you propose is
> properly the least intrusive one, so that's good. That being said, it
> would be a new (and possibly confusing) thing in the language if
> functions can have an effect *after* they have returned. This is
> essentially what you are proposing, although only for a small subset of
> languages.
>
> In C++ (or any other OO language) the standard approach would be to
> create a class with a constructor that set the value and a destructor
> that would reset it. Perhaps something like this could be done instead?
> Not sure if that would be more natural, though, as objects in Octave
> generally don't have destructors.
>

This is actually what is done internally. The closest thing in Matlab
is the onCleanup class. With that, you'd need to write

old_pso = page_screen_output (false);
c = onCleanup (@() page_screen_output (old_pso));

which is again wordy, though you at least don't need to enclose the
whole function body. In Matlab, basically any m-function can achieve
the same effect that I was proposing, by doing

function myfunc ()
  c = onCleanup (@() doSomething()); % create a cleanup action
  assignin ("caller", "c", c); % inject it into caller scope
endfunction

Ergo, it is *not* a new thing in the Matlab language that functions
can achieve effects after they're called.
Octave currently doesn't support onCleanup, but it may in the future.
I think David Bateman proposed a compiled implementation some time
ago, but JWE deferred it for later consideration. It would be also
possible to make a trivial m-file implementation if Octave supported
new-style handle classes, but there are some big obstacles associated
with new-style classes and especially handle classes, so I suppose
this won't happen anytime soon.

So, the answer to your second paragraph is no, it could not be done,
at least not now.
Note that the onCleanup version still requires you to explicitly write
both the set and the restore code (i.e. you need to write the variable
name twice), still requires you to make up a variable name for the
saved state, and also requires you to make up a varname for the
cleanup class (in that aspect it's even more difficult, given that
most programmers are bad at inventing good names for helper
variables).

Another possible problem with onCleanup is that you can inadvertently
invoke it earlier by clear or reassignment.

-- 
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]