Section BooleanAlgebra
Boolean algebra functions.
Some developpers may be disappointed by Lua false
and nil
check.
local myVar = false if myVar then print("true"); else print("false"); endThis code always prints true, because myVar is not
nil
. A good practice is always to write
if myVar == true then...or
if myVar ~= true then...
The same goes for assignments
local myVar = foo() or bar() or anotherVar == 3
If all return
nil
, myVar will be nil
, not false
. Then if you don't want nil
, the good practice is to write
myVar = ... or ... or ... or false
Even if not clearly said, but Lua has the C-based languages ternary operator:
var2 = myVar == true and "T" or "F"written in C by
var2 = myVar ? "T" : "F";
Here are some function that my help, or are syntaxic sugars in some complex case.
Use them with caution! Let's analyze this simple code:
myVar = AND(type(str) == "string", str < "Hello")
both expressions "is a string?" and "before 'Hello'?" are evaluated before the AND() function. This will produce error if str is not a string, not comparable with
"Hello"
! In loops or time consuming codes, 2 expression evaluations will slow down where in some cases the first would have returned true
.
In that case, it's better to write
myVar = type(str) == "string" and str < "Hellow".
And now a case where you absolutely don't want nil
:
dialog.getItem("button1").IsVisible = foo() and str == "Hellow"
This assignment can return
nil
, and we don't know how MyrScript will handle this, because DialogItem.IsVisible expect a boolean. Then you have 2 ways:
- you add
or false
at the end of the assignment, - you use the
bool()
function:dialog.getItem("button1").IsVisible = bool(foo() and str == "Hellow")
Summary
Return type | Function and summary |
---|---|
boolean | AND(boolean ...) AND function in boolean algebra, that accepts undefined number of arguments. |
IF(boolean expr, a, b) Short and safe way to write ternary expression. | |
boolean | NAND(boolean ...) NAND (NOT AND) function in boolean algebra, that accepts undefined number of arguments and returns true if at least one argument is not true . |
boolean | NIL(...) Return true if all arguments are nil |
boolean | NOR(boolean ...) NOR (NOT OR) function in boolean algebra, that accepts undefined number of arguments and returns true if all arguments are not true. |
boolean | NOT(boolean b) NOT function in boolean algebra. |
boolean | NOT_NIL(...) Return true if all arguments are not nil |
boolean | OR(boolean ...) OR function in boolean algebra, that accepts undefined number of arguments. |
boolean | XOR(boolean a, boolean b) XOR function in boolean algebra. |
boolean | bool(expr) Converts expr to a boolean. |
Functions
bool(expr)
Converts expr to a boolean.
Parameter | Type | Default | Description |
---|---|---|---|
expr | expr |
- Return
- boolean:
true
if expr is true,false
otherwise.
NOT(boolean b)
NOT function in boolean algebra.
Lua not x
means "not nil", so not true
is always true
. NOT(true)
returns nil
(for false) so it can be used safely in if..then statements. If you want to obtain a real false
, use the bool() function: a = bool(NOT(<expression>))
Parameter | Type | Default | Description |
---|---|---|---|
b | boolean | b |
- Return
- boolean:
true
ifb==nil or b==false
, nil
(for false) otherwise.
AND(boolean ...)
AND function in boolean algebra, that accepts undefined number of arguments.
Arguments that are not booleans are considered as false: AND(true,"123")
returns nil
.
Parameter | Type | Default | Description |
---|---|---|---|
... | boolean | Undefined number of arguments. |
- Return
- boolean:
true
if all arguments aretrue
nil
(for false) if at least one argument is nottrue
.
- Error
- if less than two arguments
OR(boolean ...)
OR function in boolean algebra, that accepts undefined number of arguments.
Arguments that are not booleans are ignored: OR("abc","123")
returns nil
Parameter | Type | Default | Description |
---|---|---|---|
... | boolean | Undefined number of arguments. |
- Return
- boolean:
true
if at least one argument istrue
nil
(for false) if no argument istrue
.
- Error
- if less than two arguments
NAND(boolean ...)
NAND (NOT AND) function in boolean algebra, that accepts undefined number of arguments and returns true
if at least one argument is not true
.
Arguments that are not booleans are considered as false: NAND(true,"123")
returns true
.
Parameter | Type | Default | Description |
---|---|---|---|
... | boolean | Undefined number of arguments. |
- Return
- boolean:
true
if at least one argument is nottrue
,nil
(for false) if all arguments aretrue
.
- Error
- if less than two arguments
NOR(boolean ...)
NOR (NOT OR) function in boolean algebra, that accepts undefined number of arguments and returns true
if all arguments are not true.
Arguments that are not booleans are considered as false: NOR("abc","123")
returns true
.
Parameter | Type | Default | Description |
---|---|---|---|
... | boolean | Undefined number of arguments. |
- Return
- boolean:
true
if all arguments are nottrue
,nil
(for false) if at least one argument istrue
.
- Error
- if less than two arguments
XOR(boolean a, boolean b)
XOR function in boolean algebra.
Parameter | Type | Default | Description |
---|---|---|---|
a | boolean | a | |
b | boolean | b |
- Return
- boolean:
true
if only one of a and b istrue
,nil
(for false) if a and b have same boolean value.
NIL(...)
Return true
if all arguments are nil
Parameter | Type | Default | Description |
---|---|---|---|
... | ... |
- Return
- boolean:
true
if all arguments arenil
,nil
(for false) if at least one argument is notnil
.
NOT_NIL(...)
Return true
if all arguments are not nil
Parameter | Type | Default | Description |
---|---|---|---|
... | ... |
- Return
- boolean:
true
if all arguments are notnil
,nil
(for false) if at least one argument isnil
.
IF(boolean expr, a, b)
Short and safe way to write ternary expression.
C-language: result = expr ? a : b;
Lua / MyrScript: result = expr == true and a or b
In a spreadsheet cell: =IF(expr;a;b) So here is Lua syntaxic sugar: result = IF(expr, a, b)
.
Parameter | Type | Default | Description |
---|---|---|---|
expr | boolean | expr | |
a | a | ||
b | b |
- Return
- a if expr is
true
, else b. - Error
- if expr is not a boolean