lua-users home
lua-l archive

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


Am 03.07.2013 18:58 schröbte Andrew Starks:
I replied to a different thread (too much splitting). To keep things
together, I will cross-post:

I'd suggest not having `nothing`.

I agree. Otherwise we would need a concept for the absence of this `nothing` value. `nothing` only makes sense in the context of a collection of values (like Lua's argument/return value lists).

We need some other way to detect `nothing` like `select('#',...)`, or `table.has()/defined()/exists()`, or raising an error when trying to access it.


         a = function() end
         b = function() return end
         c = function() return nil end

print(select('#', a()), select('#', b()), select('#', c())
--> 0, 0, 1


function
exists(...)
   return not select('#', ...) == 0
end
local foo
print(exists(foo))
-->true
print(exists(baz))
--false --currently true

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

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

print(exists(a()))
--false --currently true

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

print(exists(b()))
--false --currently true

Same here, already prints `false`.

print(exists(c()))
--true

This is not to suggest an implementation. It is only to illustrate behavior.

-Andrew

Philipp