Using a ~
character in the output list when calling a function causes the output value to be ignored.
This syntax and behavior is Matlab compatible, but AFAIK, Matlab only skips saving the value in the current workspace and provides no way for the function to know whether the output will be ignored, so there is no (easy) way to avoid computation of the value. It’s all about saving memory in the workspace of the caller.
In Octave, we have a function, isargout
that allows a function to check whether an output value will be ignored so that computation can also be avoided. It works for simple function calls. For example,
function varargout = foofun ()
varargout = cell (1, nargout);
printf ("ignored outputs:");
printf (" %d", find (! arrayfun (@isargout, 1:nargout)));
printf ("\n");
endfunction
[a, ~, b, ~] = foofun ();
will display
ignored outputs: 2 4
It also works when calling a function using a function handle or through feval, so
fh = @foofun;
[a, ~, b, ~] = fh ()
[a, ~, b, ~] = feval (fh)
[a, ~, b, ~] = feval ("foofun")
will all display the same output as above.
This feature works for feval because we don’t do anything to invalidate the list of ignored outputs in the assignment expression when calling the internal interpreter::feval
function. That’s great when you want to implement a function like feval
but not if you just want to use interpreter::feval
in your .oct
file to call an external function without inheriting the caller’s list of ignored output parameters.
I just “fixed” the mexCallMATLAB
function (see https://savannah.gnu.org/bugs/index.php?59597) so that it doesn’t propagate the prevailing isargout
info to the function it calls.
But the problem remains for anyone using Octave’s interpreter::feval
functions directly. Forcing everyone who uses interpreter::feval
to make this choice seems like trouble. I believe builtin
(which ultimately calls interpreter::feval
) would be affected similarly. Are there others?
Maybe the best fix is to add a parameter to interpreter::feval
to specify that the prevailing isargout
info should be preserved and otherwise disable it by default?
Comments, questions, and suggestions would be helpful.
Thanks.