lua-users home
lua-l archive

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

Here is another proposal (not necessary, but could give some clarity for some people):

Maybe we could add an error/custom message to every nil variable.
Plus, add of a function reason(nil_var) that give the message.

I'll give some examples to try to explain ...

local empty_table = {}

-- Example 1
local x = empty_table.x
print(x) -- nil
print(reason(x)) -- Index not found

-- Example 2
local it = pairs(empty_table)
local val = it(empty_table)
print(val) -- nil
print(reason(val)) -- End of iterator

-- Example 3
local num = tonumber("Hello World !")
print(num) -- nil
print(reason(num)) -- Cannot convert to number

-- Example 4
local var
print(var) -- nil
print(reason(var)) -- Undefined value

-- Example 5
local f1 = function() end
local f2 = function() return nil end
local f3 = function() return nil("I will not give you any result") end
print(reason(f1)) -- Undefined value
print(reason(f2)) -- User nil
print(reason(f3)) -- I will not give you any result

-- Example 6
print(nil == nil) -- true
print(nil("One error") == nil("Another error")) -- true

So nil would have the same behavior as before, plus an extra string message.
(Compatible with existing lua scripts with no changes.)

Just an idea, what do you think about it?

Regards, Julien