Difference between revisions of "Lua: Nil"

From Mario Fan Games Galaxy Wiki
m
m
Line 17: Line 17:
 
type(nil) --> "nil"</source>
 
type(nil) --> "nil"</source>
  
Although <tt>nil</tt> may signify a variable does not exist, this does not mean it will not open a new scope for the variable it is assigned to (implicitly or not), for instance:
+
Although <tt>nil</tt> may signify a variable does not exist, this does not mean it will not open a new [[Lua: Scoping|scope]] for the variable it is assigned to (implicitly or not), for instance:
 
<source lang="lua" enclose="div">var = 10 -- global variable 'var'
 
<source lang="lua" enclose="div">var = 10 -- global variable 'var'
  

Revision as of 19:29, 15 December 2009

Lua
Lua.gif
Basics
Intermediate
Advanced
XLua
Add to this template
 Standardwikimessagebox.png This article assumes the use of Lua 5.1.

Information may not be accurate or may need revision if you are using a different version.

nil is a special datatype in Lua, signifying the lack of useful data. Lua uses nil for various purposes; a nil index in a table is considered the end of the table, and function parameters that aren't used are assigned nil. nil is also the default "value" for undefined variables, and is assigned to variables that "expect" a return value but get none. nil and false are the only two values that are truly false; 0, the empty string, and an empty table are considered true in a boolean context.

nil (along with false) is often used with the or operator as an easy and clean way to assign a fallback or default value to a variable if it would otherwise be assigned nil. For example:

local x = objectName(y) or "NULL"

Assume that objectName() returns the 'name' string stored in the table of its parameter (in this case, table 'y'), or returns nil if there is no string there.

If there is actually a 'name' string contained in the table, Lua will assign the contents of that string to 'x'. If there is no 'name' string (returning false or nil), Lua will instead notice the or and assign the string to the right of it instead, making x = "NULL"; Lua will not evaluate the right-hand side of an or expression unless the left-hand side is nil or false.

nil may not be used in a table as a key nor value. Sparse arrays are possible, because the missing indices are really nil "holes". However, the length operator and other table functions may have undefined behavior with sparse arrays.

Though both nil and false evaluate to false in a boolean context, they are of course different datatypes; using the type() function on each will verify this:

type(false) --> "boolean"
type(nil) --> "nil"

Although nil may signify a variable does not exist, this does not mean it will not open a new scope for the variable it is assigned to (implicitly or not), for instance:

var = 10 -- global variable 'var'

function test1()
    print(var) -- accesses the global variable 'var'
end

function test2()
    local var
    print(var) -- accesses the local variable 'var'
end

test1() --> 10
test2() --> nil