Quirrel4.41.0

types.Bool.constructor

Method on every bool
constructor(...): any

Converts value to a bool. This is what runs when types.Bool itself is called, types.Bool(value).

Return value

false when called with no arguments. Otherwise the truthiness of value, using the same rule as if and !: only null, false, the integer 0 and the float 0.0 are false. Everything else is true, including an empty string and an empty array or table - types.Bool("") and types.Bool([]) both give true, unlike languages where an empty container or string counts as false.

Notes

Never throws: every type has a truth value, so this always succeeds. Takes zero or more arguments; a second and later argument is accepted and ignored, the same generic fallback receiver described on types.String.constructor shows up here too since the binding carries no declaration string.

Example

examples/types/bool/constructor.nut
let types = require("types")
println("types.Bool() =", types.Bool())      // no args: false
println("types.Bool(0) =", types.Bool(0))
println("types.Bool(0.0) =", types.Bool(0.0))
println("types.Bool(\"\") =", types.Bool(""))    // an empty string is still truthy
println("types.Bool([]) =", types.Bool([]))    // so is an empty array
println("types.Bool(5) =", types.Bool(5))
Output:
types.Bool() = false
types.Bool(0) = false
types.Bool(0.0) = false
types.Bool("") = true
types.Bool([]) = true
types.Bool(5) = true

See also

tointegerConverts the bool to an integer.
constructorConverts value to an integer. This is what runs when types.Integer itself is called, types.Integer(value).
constructorConverts value to a float. This is what runs when types.Float itself is called, types.Float(value).
boolclass index