lua-users home
lua-l archive

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


On Wed, Jul 3, 2013 at 10:57 AM, Sean Conner <sean@conman.org> wrote:
> It was thus said that the Great Roberto Ierusalimschy once stated:
>> > Well, I cannot speak for a 'community of best practice' but this is how I
>> > see the problem. Lua does not have null, as understood in other languages.
>>
>> I think it does: it is called 'nil'. What Lua does not have is a way to
>> store 'nil' in a table.
>
>   There are two concepts here, reflected in the following code:
>
>         x = function() end
>         y = function() return nil end
>
>   In Lua today, both x and y will nil (or in other words, they don't exist).
> But what's really going on here?  In the first case, the function returns
> nothing, so there is nothing to put into x.  In the second case, the
> function returns nil, so there *is* something to put into y, but it's a
> value that typically means "invalid" or "not applicable" or even "nothing to
> see here".
>
>   It's that last point that's problematic I think.  There is a difference
> (in my opinion) between a function that returns nothing, and a function that
> returns nil.  In Lua, "nil" plays both roles, and in a lot of cases, that's
> okay.  But there are times when having a distinction between "nothing" and
> "invalid" is critical and it's something that Lua lacks.  So right now, you
> have:
>
>         Platonic essence        Lua
>
>         nothing                 nil
>         nil                     nil
>
>   What people seem to be asking for is:
>
>         Platonic essence        Lua     NeoLua
>         nothing                 nil     empty [1]
>         nil                     nil     nil
>
>         [1] Personally, I like the term "nothing" instead of "empty" as I
>         feel it better reflects what is happening, but hey, it's a start.
>
>   (or maybe I have it backwards, that the Platonic essence of "nothing"
> should be nil, while the Platonic essence of "nil" should be empty)
>
>   Anyway, that's how I see it.
>
>   -spc (And I've been able to work around the nilless tables of Lua)
>
>

Perhaps it stems from the conflation of nouns and verbs.

You can't set a variable to "undefined". Just as you can't adequately
address a non-existant person or otherwise "do" anything to them.

Instead you "undefine" / "define a variable.

To further illustrate your point, consider the following hackary:

local pcall, select, print, type =  pcall, select, print, type
_ENV.__EMPTY = {}
setmetatable(_ENV,{
__index = function(t, i)
return t.__EMPTY
end
})

local function exists(...)
local  e =select(1, ...)
return  select("#", ...) > 0 and (type(e) == nil or e ~= _ENV.__EMPTY)
end
local foo

print(exists(bar))
--> false
print(exists( foo))
--> true
print(exists())
--> false

Better would be that select("#",...) would be `0` if I had called it with `bar`.

Then all we'd be missing is the facility to `delete` or `undefine` a variable.

-Andrew