lua-users home
lua-l archive

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


* Lorenzo Donati:

> (1) is it safe to rely on such behaviour? I.e. is it intentional or is
> it only an implementation detail that may change at any time, even
> with minor releases of Lua? Lua 5.2 still complies, but haven't found
> a clear reference to this behaviour in the manual.

It's a result of this:

| If an expression is used as the last (or the only) element of a list
| of expressions, then no adjustment is made (unless the call is
| enclosed in parentheses).

(Section 2.5)

And this:

| Otherwise, index must be the string "#", and select returns the
| total number of extra arguments it received.

(Section 5.1)

> (2) assuming that (1) is intentional and stable, I'd like to pass a
> function (call it "func") to an higher order function that will call
> it many times and do different things according to whether func
> returned no values instead of only nil values.

> function higher( func )
>   local nrets
>   local function helper( ... )
>     nrets = select( '#', ... )
>     return ...
>   end
>
>   local ret1, ret2 = helper( func() )
>   if nrets == 0 then
>     print "func returned no values"
>   else
>     print "func returned something:"
>     print( ret1, ret2 )
>   end
> end

I think you should put your logic into the helper function itself.
The result will be a bit simpler, and you can perhaps avoid creating
closures.