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"); end
This 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:
  1. you add or false at the end of the assignment,
  2. you use the bool() function:
    dialog.getItem("button1").IsVisible = bool(foo() and str == "Hellow")

See
https://en.wikipedia.org/wiki/Boolean_algebra

Summary

Return typeFunction and summary
booleanAND(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.
booleanNAND(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.
booleanNIL(...)
Return true if all arguments are nil
booleanNOR(boolean ...)
NOR (NOT OR) function in boolean algebra, that accepts undefined number of arguments and returns true if all arguments are not true.
booleanNOT(boolean b)
NOT function in boolean algebra.
booleanNOT_NIL(...)
Return true if all arguments are not nil
booleanOR(boolean ...)
OR function in boolean algebra, that accepts undefined number of arguments.
booleanXOR(boolean a, boolean b)
XOR function in boolean algebra.
booleanbool(expr)
Converts expr to a boolean.

Functions

bool(expr)

Converts expr to a boolean.

ParameterTypeDefaultDescription
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>))

ParameterTypeDefaultDescription
bboolean b
Return
boolean:
  • true if b==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.

ParameterTypeDefaultDescription
...boolean Undefined number of arguments.
Return
boolean:
  • true if all arguments are true
  • nil (for false) if at least one argument is not true.
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

ParameterTypeDefaultDescription
...boolean Undefined number of arguments.
Return
boolean:
  • true if at least one argument is true
  • nil (for false) if no argument is true.
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.

ParameterTypeDefaultDescription
...boolean Undefined number of arguments.
Return
boolean:
  • true if at least one argument is not true,
  • nil (for false) if all arguments are true.
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.

ParameterTypeDefaultDescription
...boolean Undefined number of arguments.
Return
boolean:
  • true if all arguments are not true,
  • nil (for false) if at least one argument is true.
Error
if less than two arguments

XOR(boolean a, boolean b)

XOR function in boolean algebra.

ParameterTypeDefaultDescription
aboolean a
bboolean b
Return
boolean:
  • true if only one of a and b is true,
  • nil (for false) if a and b have same boolean value.

NIL(...)

Return true if all arguments are nil

ParameterTypeDefaultDescription
...  ...
Return
boolean:
  • true if all arguments are nil,
  • nil (for false) if at least one argument is not nil.

NOT_NIL(...)

Return true if all arguments are not nil

ParameterTypeDefaultDescription
...  ...
Return
boolean:
  • true if all arguments are not nil,
  • nil (for false) if at least one argument is nil.

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).

ParameterTypeDefaultDescription
exprboolean expr
a  a
b  b
Return
a if expr is true, else b.
Error
if expr is not a boolean