Difference between revisions of "Assert"
From GiderosMobile
(added example) |
|||
Line 4: | Line 4: | ||
=== Description === | === Description === | ||
− | Issues an error when the value of its argument v is false (i.e., nil or false) | + | Issues an error when the value of its argument ''v'' is false (i.e., nil or false), otherwise returns all its arguments. |
<source lang="lua"> | <source lang="lua"> | ||
assert(v,message) | assert(v,message) | ||
</source> | </source> | ||
+ | |||
+ | ''message'' is an error message, when absent it defaults to "assertion failed!". | ||
=== Parameters === | === Parameters === | ||
'''v''': (any) expression/function to assert<br/> | '''v''': (any) expression/function to assert<br/> | ||
'''message''': (string) error message if assert fails '''optional'''<br/> | '''message''': (string) error message if assert fails '''optional'''<br/> | ||
+ | |||
+ | === Example === | ||
+ | '''Checks if a texture is valid''' | ||
+ | <source 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 | ||
+ | </source> | ||
{{(global)}} | {{(global)}} |
Revision as of 08:58, 2 March 2021
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
Example
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