lua-users home
lua-l archive

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

Am 03.07.2013 20:56 schröbte Andrew Starks:
On Wednesday, July 3, 2013, Philipp Janda wrote:

Perhaps we could make every table access a vararg expression of zero or
one value (with the usual argument adjustments) ...

     select( '#', baz, baz, baz )  --> 2

Here you are saying that # adjusts so that the last position can be read
and if the variable isn't defined then don't include it. This is how print
and other functions work, presently. Correct?

Every expression in a list of expressions is adjusted to one value, except the last one.

    function empty() end
    function multi() return 1, 2, 3 end

    select( '#', empty(), empty(), empty() )

is 2 because all return value lists of empty() are adjusted to one nil except the last call to empty()


    select( '#', multi(), multi(), multi() )

is 5 because it is equivalent to

    select( '#', 1, 1, 1, 2, 3 )

This makes assignments (or argument passing, or table construction) more predictable:

    local a, b, c = empty(), multi(), 3

c is 3, no matter what empty() or multi() return. IMHO this would be even more important if table access become vararg expression lists:

    local a, b, c = t[ 1 ], t[ 2 ], t[ 3 ]

c should be equal to t[ 3 ] even if t[ 1 ] and t[ 2 ] do not exist.

     select( '#', baz, baz, baz, foo ) --> 4
     { 1, baz, 2, baz }        --> { 1, nil, 2 }

In the case of table access, I'd suggest that "nil" would be the default
return for all values, but that you could still call an exists-like
function on any index value.

select(1,({1,baz,2,nil})[2]) --> 0
select(1,({1,baz,2,nil})[4]) --> 1

So this would be an exception to the usual argument adjustment: If inside a table constructor, do not adjust an empty list to a single nil, only increment the index.

I'm not sure this is worth it.

     local x, y = baz, 3  --> x = nil, y = 3
     t = { a = nil }
     select( '#', t.a )   --> 1
     select( '#', t.b )   --> 0

--false --currently true

This already prints `false`. The list of return values can hold nothing,
or one or more values.

Not on my copy of Lua.

--false --currently true

Same here, already prints `false`.

Again, not for me. What version are you using?

I'm sorry, I'm using:

    function exists( ... )
      return select('#', ... ) ~= 0

which is why I didn't catch the precedence bug in your version (as Matt did else-thread).