Difference between revisions of "Lua: Nil"

From Mario Fan Games Galaxy Wiki
m
m
Line 1: Line 1:
 
{{Lua}}
 
{{Lua}}
'''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 and the empty string are considered "true".
+
<tt>'''nil'''</tt> is a special datatype in [[Lua]], signifying the lack of useful data. Lua uses <tt>nil</tt> for various purposes; a <tt>nil</tt> index in a table is considered the end of the table, and function parameters that aren't used are assigned <tt>nil</tt>. <tt>nil</tt> is also the default "value" for undefined variables, and is assigned to variables that "expect" a return value but get none. <tt>nil</tt> and <tt>false</tt> are the only two values that are truly <tt>false</tt>; 0, the empty string, and an empty table are considered <tt>true</tt> 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:
+
<tt>nil</tt> (along with <tt>false</tt>) is often used with the <tt>or</tt> operator as an easy and clean way to assign a fallback or default value to a variable if it would otherwise be assigned <tt>nil</tt>. For example:
  
 
  <source lang="lua">local x = objectName(y) or "NULL"</source>
 
  <source lang="lua">local x = objectName(y) or "NULL"</source>
  
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.
+
Assume that <tt>objectName()</tt> returns the 'name' string stored in the table of its parameter (in this case, table 'y'), or returns <tt>nil</tt> 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'''.
+
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 <tt>false</tt> or <tt>nil</tt>), Lua will instead notice the <tt>or</tt> and assign the string to the right of it instead, making x = "NULL"; Lua will not evaluate the right-hand side of an <tt>or</tt> expression unless the left-hand side is <tt>nil</tt> or <tt>false</tt>.
  
Though both '''nil''' and '''false''' evaluate to ''false'', they are of course different datatypes; using the '''type()''' function on each will verify this:
+
<tt>nil</tt> may not be used in a table as a key nor value. Sparse [[array]]s are possible, because the missing indices are really <tt>nil</tt> "holes". However, the length [[Lua: Operators|operator]] and other table functions may have undefined behavior with sparse arrays.
 +
 
 +
Though both <tt>nil</tt> and <tt>false</tt> evaluate to <tt>false</tt> in a boolean context, they are of course different datatypes; using the <tt>type()</tt> function on each will verify this:
  
 
<source lang="lua">type(false) --> "boolean"
 
<source lang="lua">type(false) --> "boolean"
 
type(nil) --> "nil"</source>
 
type(nil) --> "nil"</source>

Revision as of 01:34, 26 September 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"