Search code examples
lualua-table

'nil' as an element in a Lua table?


I wonder if nil is a valid element in a table in Lua.

What I don't understand is

The following code prints 3

t = {1, 2, 3, nil};
print(#t);

But the following prints 4

t = {1, nil, 3, 4};
print(#t);

I don't understand why the two codes output different results.


Solution

  • What you're experiencing is argument trimming.

    Let's run through what you have and explain what's happening when the Lua parses it.

    -- T is equal to 1, 2, 3, (NOTHING)
    -- Therefore we should trim the nil from the end.
    t = {1, 2, 3, nil};
    
    -- T is equal to 1, nil, 3, 4
    -- We have something on the end after nil, so we'll count nil as an element.
    t = {1, nil, 3, 4};
    

    The same happens in functions, too. Which can be a bit of a hassle, but sometimes handy. Take the following for example:

    -- We declare a function with x and y as it's parameters.
    -- It expects x and y.
    function Vector(x, y) print(x, y); end
    
    -- But... If we add something unexpected:
    Vector("x", "y", "Variable");
    -- We'll encounter some unexpected behaviour. We have no use for the "Variable" we handed it.
    -- So we just wont use it.
    

    Same goes for the other way around. If you hand in a function that requires X, Y, and Z but you hand it X and Y, you'll be passing nil instead of Z.

    Refer to this answer here, because you can indeed represent nil within a table using the following:

    --  string  int   nil
    t = {"var", "1", "nil"};