Assert: Difference between revisions
From GiderosMobile
No edit summary |
No edit summary |
||
| (10 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
''' | '''Available since:''' Gideros 2011.6<br/> | ||
''' | '''Class:''' [[(global)]]<br/> | ||
=== | |||
=== Description === | |||
< | Issues an error when the value of its argument ''v'' is false (i.e.: nil or false), otherwise returns all its arguments. | ||
<syntaxhighlight lang="lua"> | |||
</ | assert(v,message) | ||
=== | </syntaxhighlight> | ||
'''v''': (any) | |||
'''message''': (string) | ''message'' is an error message, when absent it defaults to "assertion failed!". | ||
=== Parameters === | |||
'''v''': (any) expression/function to assert<br/> | |||
'''message''': (string) error message if assert fails '''optional'''<br/> | |||
=== Examples === | |||
'''Checks if a texture is valid''' | |||
<syntaxhighlight lang="lua"> | |||
function isValidTexture() | |||
local result, msg = assert(Texture.new(texturepath), "file is not recognised") | |||
print(result, msg) | |||
return result | |||
end | |||
function showTexture() | |||
if isValidTexture() then bmp = Bitmap.new(Texture.new(texturepath)) end | |||
end | |||
</syntaxhighlight> | |||
'''Checks if is of type ''number''''' | |||
<syntaxhighlight lang="lua"> | |||
local value = 12 --> 12 | |||
--local value = "hello" --> *** Friction must be a number *** | |||
assert(type(value) == 'number', "*** Friction must be a number ***") | |||
</syntaxhighlight> | |||
=== See also === | |||
'''https://luau.org/library#global-functions'''<br/> | |||
'''[[Pcall]]''' | |||
{{(global)}} | |||
Latest revision as of 10:09, 21 July 2025
Available since: Gideros 2011.6
Class: (global)
Description
Issues an error when the value of its argument v is false (i.e.: nil or false), otherwise returns all its arguments.
assert(v,message)
message is an error message, when absent it defaults to "assertion failed!".
Parameters
v: (any) expression/function to assert
message: (string) error message if assert fails optional
Examples
Checks if a texture is valid
function isValidTexture()
local result, msg = assert(Texture.new(texturepath), "file is not recognised")
print(result, msg)
return result
end
function showTexture()
if isValidTexture() then bmp = Bitmap.new(Texture.new(texturepath)) end
end
Checks if is of type number
local value = 12 --> 12
--local value = "hello" --> *** Friction must be a number ***
assert(type(value) == 'number', "*** Friction must be a number ***")
See also
https://luau.org/library#global-functions
Pcall