[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Odd behaviour with multiple return values and varargs
- From: Miles Bader <miles.bader@...>
- Date: Fri, 14 Sep 2007 11:30:00 +0900
I think you're misunderstanding the nature of multiple return values in
Lua, in two ways.
1. I think in general there's a notion that MRVs are "the main RV(s) plus
some other stuff which you might want to ignore". This is sort of like
in common-lisp; contrast it with "rigid" MRVs such as are I guess often
used with scheme.
This is essentially a style issue I suppose , but the language
definitely supports this view -- return values are "flexible" in the
sense that the system will automatically discard unwanted values (the
most common case, of course is where the user asks for only one, and
extra values are discarded), and will even fill in asked-for but not
2. There's a notion in Lua of unsupplied/non-existant == nil, and one might
think of every function as having an infinite number of arguments (with
any unsupplied arguments having the value nil), or of every table as
having an infinite number of entries (with all non-existing entries
having a value of nil). Similarly, one could think of every function
returning an infinite number of values.
So the notions of "number of arguments","number of table entries", or
"number of return values" are kind of shaky, even if such information
actually is available in some cases, e.g. with select('#',...).
If we think of a return value sequence as having an implicit
infinite-number-of-nils at the end, then it's still perfectly natural to
propagate such a MRV sequence in the last argument position (or as the
last thing in a table initialization), because the
infinite-number-of-nils is still at the end. In any _other_ position,
it just doesn't work.
You could make it work if you had (as someone suggested) a "first N
values" operator, which would let you specify exactly how many values
from that infinite sequence you wanted. Currently of course only the
N=1 case is supported by implicitly discarding all but the first value
in non-final positions (or by parenthesizing in the final position).
`There are more things in heaven and earth, Horatio,
Than are dreamt of in your philosophy.'