Boolean evaluation - Manhunter07/MFL GitHub Wiki

This article describes the way, MFL asserts truthy and falsy values. Although this might remind of the True and False constants, they are not the only truthy or falsy values and thus only losely connected to this topic.

Boolean evaluation of boolean and non-boolean is used in a variety of situations, including if-then-else or case-of-then-else statements, in comparisons with the ? operator or in the assert command. Unlike many weakly-typed languages like JavaScript, MFL although treating these values as falsy or truthy does not auto-convert them into each other, thus does not end up with logical inconsistencies: There is no implicit conversion between data types unless you declare a converter.

The reason that truthy and falsy values exist is simple: In duck-typed languages, it is often pointless to expect the programmer to convert all values to boolean, if one could simply also accept non-booleans in for example conditional statements. This saves both coding time, execution time and characters, as we will see below.

Truthyness

Truthyness describes the state of being interpreted as an assigned, non-empty and true-like value. Generally speaking, values are considered to be truthy if they are not explicitely defined as falsy. This means that unlike a falsy value which is explicitely defined for each data type, truthy values are simply considered to be any values other than these. This includes True, but also non-Nil references, non-zero numbers and non-empty strings and other structures.

Falsyness

Falsyness describes the state of being interpreted as an unassigned, empty or false-like value. Unlike the set of truthy values, the set of falsy ones can be clearly defined and is finite. The following values are treated as falsy by the compiler during evaluation.

Data type Falsy value(s) Description
Null values Null Since Null is the only possible value here, null values are always falsy
References Nil Unassigned references are treated as falsy
Booleans False While True is considered truthy, False is considered falsy
Numbers 0 Floating-point numbers that are considered as zero are treated as falsy
Strings "" Strings with a length of zero characters are considered falsy
Arrays [] Arrays with a length of zero elements are considered falsy
Records {} Records without fields are treated as falsy

Strings with only spaces or null-characters, arrays with only falsy values as elements and records with only falsy values as field values are not falsy.

⚠️ **GitHub.com Fallback** ⚠️