octave-bug-tracker
[Top][All Lists]
Advanced

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

[Octave-bug-tracker] [bug #65116] classdef display override populates an


From: Rik
Subject: [Octave-bug-tracker] [bug #65116] classdef display override populates ans with class when using inputname
Date: Thu, 4 Jan 2024 20:56:23 -0500 (EST)

Follow-up Comment #4, bug#65116 (group octave):

I independently arrived at the same conclusion.  The code is in inputname.m. 
The relevant line is


 87     namestr = evalin ("caller", sprintf ("__varval__('.argn.'){%d}", n));


The help text for eval says


Parse the string TRY and evaluate it as if it were an Octave program.


Octave is doing the documented thing which is to treat the code string exactly
as any other program.  And when you call a function which has an output, and
don't assign it to anything, then it the variable 'ans' gets that value.

eval is *supposed* to have side effects.  Something like


evalin ('caller', 'x = 5');


will create a variable 'x' in the calling workspace.  It seems consistent that
a command that would normally assign to "ans" does that still when called
through eval().

First thought is that we don't change anything.  This is a very small issue,
and "ans" is a volatile variable anyways changing frequently.

Second easy way out would be to use evalin a second time to clear the "ans"
variable.  This wouldn't be perfect because if the variable had existed before
the call to inputname it would now be missing.

Third way out would be to assign to a temporary variable in the caller
workspace and then clear that variable.  An arbitrarily weird variable name
should be fine.

Fourth way is to avoid calling bind_ans in eval_string() in pt-eval.cc. 


1064                   bool do_bind_ans = false;
1065
1066                   if (expr->is_identifier ())
1067                     do_bind_ans = ! is_variable (expr);
1068                   else
1069                     do_bind_ans = ! expr->is_assignment_expression ();
1070
1071                   if (do_bind_ans && ! retval.empty ())
1072                     bind_ans (retval(0), expr->print_result ());


This would probably mean altering the API for eval_string to pass a special
flag down from evalin(), possibly undocumented.  Or inputname could be
re-written into C++ and pass the flag directly.  Or, we make a special
evalin_inputname function which doesn't call bind_ans and is only meant to be
called from inputname.m.






    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?65116>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/




reply via email to

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