Difference between revisions of "Lua: Nil"

From Mario Fan Games Galaxy Wiki
m
(Cleaning and clarifying)
 
Line 1: Line 1:
 
{{Lua}}
 
{{Lua}}
<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.
 
  
<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:
+
= Nil =
 +
'''Nil''' is a special datatype in Lua. It is a symbol representing that there is no data at all. __NOTOC__
  
<source lang="lua" enclose="div">local x = objectName(y) or "NULL"</source>
+
The <tt>nil</tt> index of a [[Lua: Tables|table]] signifies the end of said table, and any <tt>function</tt> parameter that is not used or does not exist is also <tt>nil</tt>. Undefined variables within expressions, or defined variables with no value assigned, are assumed to be <tt>nil</tt>, as are variables set to the result of a function with no return value.
  
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.
+
<tt>nil</tt> is not valid as a key or value within a <tt>table</tt>, although missing indices within an array will be considered <tt>nil</tt>.
  
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>.
+
<tt>nil</tt> is the only valid substitute for the <tt>boolean</tt> value ''false''; 0 and any "empty" value (such as the "" string) are still ''true'' in <tt>boolean</tt> comparison.
  
<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.
+
= Behavior of nil =
 +
=== Use in conditional statements ===
 +
<tt>nil</tt> may be used with the <tt>or</tt> operator to create shortcuts in code:
 +
<source lang="lua" enclose="div">local x = objectName(y) or "NULL"</source>
  
Though both <tt>nil</tt> and <tt>false</tt> [[Lua: Coercion|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:
+
Assume that ''objectName()'' returns the name <tt>string</tt> in its <tt>table</tt> y.
 +
 
 +
If there is such a name <tt>string</tt>, the x variable shall now contain it.
 +
 
 +
Otherwise, ''false'' or more likely <tt>nil</tt> are returned. In this case - and only in this csae - Lua will respond to the <tt>or</tt> operator and assign the <tt>string</tt> to its right instead, this being "NULL".
 +
 
 +
=== Type() function ===
 +
Although <tt>nil</tt> and ''false'' are generally equivalent, <tt>nil</tt> is its own type, whereas ''false'' is a <tt>boolean</tt> value, as seen with the ''type()'' function:
  
 
<source lang="lua" enclose="div">type(false) --> "boolean"
 
<source lang="lua" enclose="div">type(false) --> "boolean"
 
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 [[Lua: Scoping|scope]] for the variable it is assigned to (implicitly or not), for instance:
+
=== Scope ===
 +
Declaring a global variable with a value, declaring a local variable of the same name with no value, and returning this local varabile, will return <tt>nil</tt>:
 +
 
 
<source lang="lua" enclose="div">var = 10 -- global variable 'var'
 
<source lang="lua" enclose="div">var = 10 -- global variable 'var'
  
Line 25: Line 37:
  
 
function test2()
 
function test2()
     local var
+
     local var -- Defines a new local variable, also named 'var'
     print(var) -- accesses the local variable 'var'
+
     print(var) -- accesses the local 'var', not the global one
 
end
 
end
  
test1() --> 10
+
test1() --> 10; the global 'var' was assigned this value
test2() --> nil</source>
+
test2() --> nil; the local 'var' was not assigned any value</source>

Latest revision as of 03:53, 23 December 2010

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

Nil is a special datatype in Lua. It is a symbol representing that there is no data at all.

The nil index of a table signifies the end of said table, and any function parameter that is not used or does not exist is also nil. Undefined variables within expressions, or defined variables with no value assigned, are assumed to be nil, as are variables set to the result of a function with no return value.

nil is not valid as a key or value within a table, although missing indices within an array will be considered nil.

nil is the only valid substitute for the boolean value false; 0 and any "empty" value (such as the "" string) are still true in boolean comparison.

Behavior of nil

Use in conditional statements

nil may be used with the or operator to create shortcuts in code:

local x = objectName(y) or "NULL"

Assume that objectName() returns the name string in its table y.

If there is such a name string, the x variable shall now contain it.

Otherwise, false or more likely nil are returned. In this case - and only in this csae - Lua will respond to the or operator and assign the string to its right instead, this being "NULL".

Type() function

Although nil and false are generally equivalent, nil is its own type, whereas false is a boolean value, as seen with the type() function:

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

Scope

Declaring a global variable with a value, declaring a local variable of the same name with no value, and returning this local varabile, will return nil:

var = 10 -- global variable 'var'

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

function test2()
    local var -- Defines a new local variable, also named 'var'
    print(var) -- accesses the local 'var', not the global one
end

test1() --> 10; the global 'var' was assigned this value
test2() --> nil; the local 'var' was not assigned any value