Assertions are special statements than check and ensure that certain conditions are met prior to proceeding with the execution of a function. An assertion is always declared with an expression:

assert expression

If the expression above evaluates to a true value or is equivalent to true, the statement does nothing. However, if expression is not considered true, the function will return and, if the function is declared as having a return value, a failure value will be returned (this is equivalent to executing a "return fail" statement, see above for "Failure literals"). Therefore, the following are equivalent:

func myFunction(parameter as object) as bool
{
	if parameter == null:
		return false
	return true
}

AND

func myFunction(parameter as object) as bool
{
	assert parameter
	return true
}

An assertion may also be declared with an associated error block, as follows:

assert expression {
	doSomething()
}

The block is then executed in cases where the assertion fails. Therefore, the following are equivalent:

func myFunction(parameter as object) as bool
{
	if parameter == null {
		PRINT "Parameter is null"
		return false
	}
	return true
}

AND

func myFunction(parameter as object) as bool
{
	assert parameter {
		PRINT "Parameter is null"
	}
	return true
}

As a special case of assertions, as mentioned above in the discussion about variable declarations, a variable may also be declared using the "assert" keyword, as follows:

var myVariable = assert getValueForTheVariable()

The use of "assert" in a variable declaration will cause the value of the expression to be evaluted, causing the execution of the function to end if the value evaluates to a false status. The above code is, therefore, equivalent to the following:

var myVariable = getValueForTheVariable()
if myVariable == null {
	return false
}

Twitter Facebook LinkedIn Youtube Slideshare Github