Section Syntax

Extend Lua language.

See
http://lua-users.org/wiki/SwitchStatement

Summary

Return typeFunction and summary
 IncludeOnce(string file)
Include file only and only if it hasn't been included before.
 assertDialog(dialog, string errorMessage)
Assert dialog is a Dialog, else throw errorMessage.
 assertEquals(expr, expected, string Text)
Assertion an expression or function call equals an expected result.
 assertExpression(expr, string errorMessage)
Assert an expression result is true.
 assertFalse(b, string errorMessage)
Assert b is false, else throw errorMessage.
 assertFunction(f, string errorMessage)
Assert f is a function, else throw errorMessage.
 assertNegativeNumber(n, string errorMessage)
Assert n is a negative number, else throw errorMessage.
 assertNil(n, string errorMessage)
Assert n is nil, else throw errorMessage.
 assertNotEmptyString(s, string errorMessage)
Assert s is a not-nil and not empty string, else throw errorMessage.
 assertNotNil(n, string errorMessage)
Assert n is not nil, else throw errorMessage.
 assertNotZero(n, string errorMessage)
Assert n is a number but not 0, else throw errorMessage.
 assertNumber(n, string errorMessage)
Assert n is a not-nil number, else throw errorMessage.
 assertPositiveNumber(n, string errorMessage)
Assert n is a positive number, else throw errorMessage.
 assertScore(s, string errorMessage)
Assert s is a Score, else throw errorMessage.
 assertString(s, string errorMessage)
Assert s is a not-nil string (possibly empty), else throw errorMessage.
 assertTable(t, string errorMessage)
Assert t is a table, else throw errorMessage.
 assertTrue(b, string errorMessage)
Assert b is true, else throw errorMessage.
 assertType(t, string expectedType, string errorMessage)
Assert type(t) is a expectedType, else throw errorMessage.
tablecase(string _type, _params, function _func, bool _break)
Create a case for switch() function.
tabledefault(function _func)
Create a default case for switch() function.
?switch(term, table cases)
switch..case.
 throw(string exception)
Log (console and/or file) and stop the script with error().

Functions

IncludeOnce(string file)

Include file only and only if it hasn't been included before.

For that, you must always use IncludeOnce instead of Include. Include is safe and fast for simple script that create constants and functions, but may load the CPU if included file performs heavy tasks.

ParameterTypeDefaultDescription
filestring Filename to include, file extension is not mandatory.
Usage
IncludeOnce("SMJavaCollections")

throw(string exception)

Log (console and/or file) and stop the script with error().

Inspired from Java's throw new Exception(...) but without 'catch' mechanism in Lua.

ParameterTypeDefaultDescription
exceptionstring Error message
See
err

assertNumber(n, string errorMessage)

Assert n is a not-nil number, else throw errorMessage.

ParameterTypeDefaultDescription
n   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected number, got ..." will be concatenated.
See
assertPositiveNumber
assertNegativeNumber
assertNotZero

assertPositiveNumber(n, string errorMessage)

Assert n is a positive number, else throw errorMessage.

ParameterTypeDefaultDescription
n   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected positive number, got ..." will be concatenated.
See
assertNumber
assertNegativeNumber
assertNotZero

assertNegativeNumber(n, string errorMessage)

Assert n is a negative number, else throw errorMessage.

ParameterTypeDefaultDescription
n   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected negative number, got ..." will be concatenated.
See
assertNumber
assertPositiveNumber
assertNotZero

assertNotZero(n, string errorMessage)

Assert n is a number but not 0, else throw errorMessage.

ParameterTypeDefaultDescription
n   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected not-zero number, got ..." will be concatenated.
See
assertNumber
assertPositiveNumber
assertNegativeNumber

assertString(s, string errorMessage)

Assert s is a not-nil string (possibly empty), else throw errorMessage.

ParameterTypeDefaultDescription
s   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected string, got ..." will be concatenated.
See
assertNotEmptyString

assertNotEmptyString(s, string errorMessage)

Assert s is a not-nil and not empty string, else throw errorMessage.

ParameterTypeDefaultDescription
s   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected not-empty string, got ..." will be concatenated.
See
assertString

assertTrue(b, string errorMessage)

Assert b is true, else throw errorMessage.

ParameterTypeDefaultDescription
b   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected true, got ..." will be concatenated.
See
assertFalse
assertNotNil
assertExpression

assertFalse(b, string errorMessage)

Assert b is false, else throw errorMessage.

Note: if you check a expression, e.g. x < y, Lua returns nil if the comparaison fails, instead of false.

ParameterTypeDefaultDescription
b   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected false, got ..." will be concatenated.
See
assertTrue
assertNil
assertExpression

assertTable(t, string errorMessage)

Assert t is a table, else throw errorMessage.

ParameterTypeDefaultDescription
t   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected table, got ..." will be concatenated.

assertFunction(f, string errorMessage)

Assert f is a function, else throw errorMessage.

ParameterTypeDefaultDescription
f   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected function, got ..." will be concatenated.

assertNil(n, string errorMessage)

Assert n is nil, else throw errorMessage.

ParameterTypeDefaultDescription
n   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected nil, got ..." will be concatenated.
See
assertNotNil
assertFalse
assertExpression

assertNotNil(n, string errorMessage)

Assert n is not nil, else throw errorMessage.

ParameterTypeDefaultDescription
n   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected not nil, got ..." will be concatenated.
See
assertNil
assertTrue
assertExpression

assertExpression(expr, string errorMessage)

Assert an expression result is true.

This is the equivalent of Lua assert function with more logging.

ParameterTypeDefaultDescription
expr  Result of an expression, e.g. x < y
errorMessagestring Nothing is concatenated, the message must be full and self-explainatory.
See
assertTrue
assertNotNil

assertEquals(expr, expected, string Text)

Assertion an expression or function call equals an expected result.

This is useful for unit tests.

ParameterTypeDefaultDescription
expr  Result of an expression
expected  Expected result
Textstring to print on console and throw if expr ~= expected
Error
If expr ~= expected

assertDialog(dialog, string errorMessage)

Assert dialog is a Dialog, else throw errorMessage.

ParameterTypeDefaultDescription
dialog   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected Dialog, got ..." will be concatenated.

assertScore(s, string errorMessage)

Assert s is a Score, else throw errorMessage.

ParameterTypeDefaultDescription
s   
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected Score, got ..." will be concatenated.

assertType(t, string expectedType, string errorMessage)

Assert type(t) is a expectedType, else throw errorMessage.

ParameterTypeDefaultDescription
t   
expectedTypestring  
errorMessagestring Message to throw: by convention, the function where it happen, and the argument's or variable's name.
": expected 'expectedType', got ..." will be concatenated.
See
assertDialog
assertFunction
assertNumber
assertScore
assertString
assertTable
Example
assertType(s, "Staff", "MyFunction, argument #1")
--> if s is not a Staff, this print
--> ERROR MyFunction, argument #1, expected Staff, got ...

switch(term, table cases)

switch..case.

Keyword switch doesn't exist in Lua? Here it is. Each case can test more things than C-like equals: equals, not equals, comparison (greater, lower), in a table, between two values (range). See SwitchCaseTest() for examples. Note that it creates tables, so switch is not best solution in huge loops where a set of if..elseif..elseif..end can do the job faster.

ParameterTypeDefaultDescription
term  string or number, that can be compared
casestable Table of cases created by case() and default() functions.
Return
a value if cases' "body" functions return a value.
Usage
switch(term, { case(...), case(...), default(...) }).
Example
local result = switch(my_number, {
case("<", 0, function(term) print(term.." is negative") return "negative" end),
case("eq", 2, function(term) print(term.." = two") return "=2" end, false),
case("in", {3,5,7,11,13,17}, function(term) print(term.." is a prime") return "prime" end),
case("range", {20,29}, function(term) print("20 <= "..term.." <= 29") return "between 20 and 29" end),
default( function(term) print(term.." doesn't match any case, this is default") return "The answer is... 42" end)
})

case(string _type, _params, function _func, bool _break)

Create a case for switch() function.

ParameterTypeDefaultDescription
_typestring Accepted values are:
  • "=", "==", "eq" for equals,
  • "~=", "!=", "~eq", "!eq" for not equals,
  • "<", "<=", ">", ">=", "lt", "le", "gt", "ge" for comparison,
  • "in", "!in", "range", "!range"
_params  A number, a string, or a table:
  • For "in" and "!in" table can have 1 to n items.
  • For "range" and "!range" table must have 2 items.
_funcfunction  
_breakbooltruetrue to stop after this case, false to continue and execute function of next case(s) until a case breaks.
Return
table: a case for switch() function
See
switch

default(function _func)

Create a default case for switch() function.

ParameterTypeDefaultDescription
_funcfunction that will receive the tested term as parameter
Return
table: a case for switch() function
See
switch