Difference between revisions of "Assert"

From GiderosMobile
(added example)
m (Text replacement - "<source" to "<syntaxhighlight")
 
(One intermediate revision by the same user not shown)
Line 5: Line 5:
 
=== Description ===
 
=== Description ===
 
Issues an error when the value of its argument ''v'' is false (i.e.: nil or false), otherwise returns all its arguments.
 
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">
+
<syntaxhighlight lang="lua">
 
assert(v,message)
 
assert(v,message)
</source>
+
</syntaxhighlight>
  
 
''message'' is an error message, when absent it defaults to "assertion failed!".
 
''message'' is an error message, when absent it defaults to "assertion failed!".
Line 17: Line 17:
 
=== Examples ===
 
=== Examples ===
 
'''Checks if a texture is valid'''
 
'''Checks if a texture is valid'''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
function isValidTexture()
 
function isValidTexture()
 
local result, msg = assert(Texture.new(texturepath), "file is not recognised")
 
local result, msg = assert(Texture.new(texturepath), "file is not recognised")
Line 27: Line 27:
 
if isValidTexture() then bmp = Bitmap.new(Texture.new(texturepath)) end
 
if isValidTexture() then bmp = Bitmap.new(Texture.new(texturepath)) end
 
end
 
end
</source>
+
</syntaxhighlight>
  
 
'''Checks if is of type ''number'''''
 
'''Checks if is of type ''number'''''
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
local value = 12 --> 12
 
local value = 12 --> 12
 
--local value = "hello" --> *** Friction must be a number ***
 
--local value = "hello" --> *** Friction must be a number ***
 
assert(type(value) == 'number', "*** Friction must be a number ***")
 
assert(type(value) == 'number', "*** Friction must be a number ***")
</source>
+
</syntaxhighlight>
  
 
=== See also ===
 
=== See also ===

Latest revision as of 17:23, 12 July 2023

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

Pcall