Difference between revisions of "Lua: Coercion"
m |
m |
||
Line 2: | Line 2: | ||
'''Coercion''' is the automatic conversion of one datatype into another. Generally, Lua attempts this when some operation is carried on one or more [[Lua: Datatypes|datatypes]]; the operation itself determines what type it will try to convert to, eg. [[Lua: Numbers|numbers]] for '+' and [[Lua: Strings|strings]] for '[[Concatenation|..]]'. If coercion fails, Lua may issue an error if the original datatype is not expected by the operation. | '''Coercion''' is the automatic conversion of one datatype into another. Generally, Lua attempts this when some operation is carried on one or more [[Lua: Datatypes|datatypes]]; the operation itself determines what type it will try to convert to, eg. [[Lua: Numbers|numbers]] for '+' and [[Lua: Strings|strings]] for '[[Concatenation|..]]'. If coercion fails, Lua may issue an error if the original datatype is not expected by the operation. | ||
− | As a special note, in | + | As a special note, in a few instances, Lua will ''not'' attempt coercion: table indexing and comparisons. In these cases, the <tt>tostring()</tt> and <tt>tonumber()</tt> functions must be used if you know the datatypes may be different. The equality operator <tt>==</tt> will accept two differing types, but will always return false; likewise, <tt>~=</tt> will always return true. The inequality operators <tt><</tt>, <tt><=</tt>, <tt>></tt>, and <tt>>=</tt> cannot compare differing types and will issue an error. As, for example, "2" and 2 are different entries in a table, coercion would more than likely be undesired. Lua will only coerce numbers during concatenation; other types must be explicitly converted with <tt>tostring()</tt>. |
===Coercion types=== | ===Coercion types=== |
Revision as of 22:19, 14 November 2009
Lua |
---|
Basics |
Intermediate |
Advanced |
|
XLua |
Add to this template |
Coercion is the automatic conversion of one datatype into another. Generally, Lua attempts this when some operation is carried on one or more datatypes; the operation itself determines what type it will try to convert to, eg. numbers for '+' and strings for '..'. If coercion fails, Lua may issue an error if the original datatype is not expected by the operation.
As a special note, in a few instances, Lua will not attempt coercion: table indexing and comparisons. In these cases, the tostring() and tonumber() functions must be used if you know the datatypes may be different. The equality operator == will accept two differing types, but will always return false; likewise, ~= will always return true. The inequality operators <, <=, >, and >= cannot compare differing types and will issue an error. As, for example, "2" and 2 are different entries in a table, coercion would more than likely be undesired. Lua will only coerce numbers during concatenation; other types must be explicitly converted with tostring().
Coercion types
Any datatype may be coerced into a boolean. nil and false will be treated as false; true and any other datatype is true regardless of their value.
Any datatype may be coerced into a string, either automatically or with tostring(). Numbers, strings, booleans, and nil will appear exactly as they would in the source code. Userdata, functions, threads, and tables will instead print out their type followed by their memory address; in the case of tables, their __tostring metamethod can be used to change how they are converted into a string.
Strings may or may not be coercible into numbers, automatically or with tonumber(); in order for this to be possible, the string must follow Lua's number literal rules. tonumber will return nil if it cannot convert a type.