lua-users home
lua-l archive

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




On Wednesday, July 3, 2013, Philipp Janda wrote:
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

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? 



    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

    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.

Not on my copy of Lua. 
print(exists(b()))
--false --currently true

Same here, already prints `false`.

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

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

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

-Andrew 

Philipp