Garry's Mod Wiki

Revision Difference

no_value#527775

<cat>Dev.Lua</cat> no value is a type in Lua representing no data. While this mostly appears the same as <page>nil</page> from the Lua state, C functions will consider this distinct. This difference can be displayed from Lua with the <page>Global.type</page> function which, unlike [vanilla Lua's type function](https://www.lua.org/pil/2.html), will return "no value" when given no data. ⤶ Examples of the difference:⤶ ⤶ ```⤶ print(type())⤶ print(type(nil)) ```⤶ ⤶ Returns:<br/>no value<br/>nil⤶ ⤶ In vanilla Lua 5.1-5.3, calling <page>Global.type</page>() will result in the error "bad argument #1 to 'type' (value expected)" instead of returning "no value", making this property unique to GLua.⤶ no value is a type in Lua representing no data. While this mostly appears the same as <page>nil</page> from the Lua state, C functions will consider this distinct. This difference can be displayed from Lua with the <page>Global.type</page> function which, unlike [vanilla Lua's type function](https://www.lua.org/pil/2.html), will return `no value` when given no data. ⤶ <example>⤶ <description>Examples of the difference:</description>⤶ <code>⤶ print( type() ) print( type( nil ) )⤶ </code>⤶ <output><br/>no value<br/>nil</output>⤶ </example>⤶ ⤶ In vanilla Lua 5.1-5.3, calling <page>Global.type</page>() will result in the error `bad argument #1 to 'type' (value expected)` instead of returning `no value`, making this property unique to GLua.⤶ Functions that return nothing technically return no value, making `return` and `return nil` unique statements. ⤶ Example of this difference:⤶ ⤶ ```⤶ ⤶ <example>⤶ <description>Examples of the difference:</description>⤶ <code>⤶ local function a() end -- Same as a local function b() return end -- Difference from a and b local function c() return nil end print(type(a())) print(type(b())) print(type(c())) ```⤶ ⤶ Returns:<br/>no value<br/>no value<br/>nil⤶ print( type( a() ) ) print( type( b() ) ) print( type( c() ) ) </code>⤶ <output><br/>no value<br/>no value<br/>nil</output>⤶ </example>⤶ Lua variables cannot hold no value and instead will default to <page>nil</page>. ⤶ Example of this behaviour:⤶ ⤶ ```⤶ ⤶ <example>⤶ <description>Example of this behaviour:</description>⤶ <code>⤶ local function a() end local d = a() print(type(d)) ```⤶ ⤶ Returns:<br/>nil⤶ print( type( d ) ) </code>⤶ <output>nil</output>⤶ </example>⤶ no value will be coerced to nil in value comparisons. ⤶ Example of this behaviour:⤶ ⤶ ```⤶ ⤶ <example>⤶ <description>Example of this behaviour:</description>⤶ <code>⤶ local function a() end print(a() == nil) print(not a()) ```⤶ ⤶ Returns:<br/>true<br/>true print( a() == nil ) print( not a() ) </code>⤶ <output><br/>true<br/>true</output>⤶ </example>