octave-maintainers
[Top][All Lists]
Advanced

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

Matlab-style empty output/input arguments support


From: Jaroslav Hajek
Subject: Matlab-style empty output/input arguments support
Date: Wed, 27 Jan 2010 13:03:19 +0100

hi all,

today I have discovered that Matlab now allows an user to explicitly
ignore selected output/input arguments:
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#br67dkp-1

In short, you can do
[x, ~, z] = my_func_3_outputs (...)

as well as

function a = my_func_3_inputs (x, ~, z)
...
endfunction

so that the ignored value doesn't pollute the local scope by using
some weirdo __xxx__ variable. In case of outputs, this has also the
advantage that the variable's memory is freed without an explicit
clear.

I wanted this feature for months, so I went straight for implementing
it, and here it is:
http://hg.savannah.gnu.org/hgweb/octave/rev/37a08e0ce2dc

So we can now do:

octave:1> [~, y] = deal (1, 2);
octave:2> who
Variables in the current scope:

ans  y

octave:3> y
y =  2
octave:4> f = @(~, y) disp (y)
f =

@(~, y) disp (y)

octave:5> f(1, 2)
 2
octave:8> function a = g (x, ~, y = 1), a = x+y; endfunction
octave:9> g(1,2,3)
ans =  4

Now some open problems:
1. Currently, you can use equally ~ and ! for the placeholder. This
was the easiest way because lex treats these two symbols as equal. If
anyone thinks this is a bad idea, it would require modifying the lexer
code.
2. You can't use ~ in simple assignment. I'm not sure whether you can
in Matlab (needs testing the recent version), but even if you can, it
seems useless to me.
3. In Octave, even the multiple assignment is an expression, so you
can do [c,d] = [a,~] = myfun (...).
Also, the values will be displayed like this:

octave:10> [~, y] = deal (1, 2)
~ =  1
y =  2

I solved this by rewriting octave_lvalue in such a manner that the
value is kept throughout the object's lifetime although not assigned
to any physical lhs, so that it can be queried again.

In the future (not 3.4) I would like to implement some optimizations
regarding the lhs values, and it would be nice if a compiled function
could omit computing the value at all if it is ignored (in the same
manner as it distinguishes multiple nargout values). But what to do in
the above cases?
Should
[c,d] = [a,~] = myfun (...)
simply raise an error?

enjoy!

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