Capy-Bearer Documentation

bool

lib call

Capy

function bool(value : any, opt : any = {}) bool

bool() converts an any to a Boolean value. opt.fallback is the result when conversion fails. It defaults to false. An empty or missing any fails conversion and uses the fallback. Use value? before conversion when presence matters.

bool() deliberately keeps an any first parameter. Strings cannot be conditions. A string must pass through any() before conversion, so if "true" remains a compile error.

Numeric zero converts to false. A finite nonzero number converts to true. A map or list uses the fallback, even when it contains entries.

Text accepts true, yes, on, and 1 as true, and false, no, off, and 0 as false. These words ignore case and surrounding whitespace. Decimal integer text also converts by zero or nonzero value. Unrecognized text uses the fallback. Thus bool(any("unknown")) is false, not a test for nonempty text. Use string(value) != "" to test for nonempty text.

The options map accepts only fallback. An unknown key, including base, is an error.

print(bool(any("YES")), "\n")
print(bool(any("unknown"), fallback: true), "\n")

Output

true
true

Parameters

value : dynamic value to convert

opt : options map with fallback

Return Values

A Boolean value, or opt.fallback.