bool
func bool(x: bool): boolbool returns the bool x holds: x is a bool or a named primitive over bool, which it unwraps.
Details
booldoes not convert other kinds. There is no truthiness: a number, a string, ornullis a compile error. Compare instead, such asn != 0ors == "true".- A named primitive type over
boolis not abool. A condition,!,&&,||, andassertneedbool(x)to use its value.
Basic examples
Unwrap a value of a named primitive type over bool:
type Verified = bool
let v = Verified(true)
v // Verified(true)
bool(v) // trueOn a plain bool, bool is the identity:
bool(true) // true
bool(false) // falseScope
bool is a value, so it can be passed to a function. It unwraps every element:
type Enabled = bool
let flags = [Enabled(true), Enabled(false), Enabled(true)]
flags.map(bool) // [true, false, true]As a predicate, it keeps the tagged elements whose payload is true:
type Enabled = bool
let flags = [Enabled(true), Enabled(false), Enabled(true)]
flags.filter(bool) // [Enabled(true), Enabled(true)]
flags.countWhere(bool) // 2Applications
Use a tagged flag in a condition:
type Verified = bool
type User = { name: string, verified: Verified }
func badge(u: User): string {
if bool(u.verified) { "${u.name} (verified)" } else { u.name }
}
println(badge(User{ name: "Ann", verified: Verified(true) }))
println(badge(User{ name: "Bob", verified: Verified(false) }))Ann (verified)
BobCount the features that are on in a settings map:
type On = bool
let features = Map{ "search": On(true), "export": On(false), "sync": On(true) }
let on = features.values().countWhere(bool)
println("${on} of ${len(features)} features on")2 of 3 features onProperties and relations
bool gives back the payload a constructor wrapped:
type Flag = bool
for b in [true, false] {
assert(bool(Flag(b)) == b)
}Two tagged values are equal exactly when their payloads are:
type Flag = bool
let a = Flag(true)
let b = Flag(false)
assert((a == b) == (bool(a) == bool(b)))Possible issues
bool is not a truthiness test. A number is a compile error; compare it instead:
let n = 3
let some = bool(n) // error: bool converts only a bool or a named primitive type over bool, got intlet n = 3
n != 0 // truebool does not parse a string. Compare the text:
let s = "true"
let b = bool(s) // error: bool converts only a bool or a named primitive type over bool, got stringlet s = "true"
s == "true" // trueA tagged value cannot be a condition or an operand of &&, ||, or !. Unwrap it first:
type Enabled = bool
let e = Enabled(true)
if e { println("on") } // error: cannot use Enabled where bool is expectedtype Enabled = bool
let e = Enabled(true)
bool(e) && !false // trueYou cannot compare a tagged value with a plain bool with ==; unwrap it first:
type Enabled = bool
let same = Enabled(true) == true
// error: Enabled and bool cannot be compared with ==: neither is assignable to the othertype Enabled = bool
bool(Enabled(true)) == true // truePrinting or interpolating a tagged value shows the constructor; unwrap it to get true/false:
type Verified = bool
"${Verified(true)}" // "Verified(true)"type Verified = bool
"${bool(Verified(true))}" // "true"A nullable bool must be narrowed first, with ?? or a null check:
let maybe: bool | null = null
let b = bool(maybe)
// error: bool converts only a bool or a named primitive type over bool, got bool | nulllet maybe: bool | null = null
maybe ?? false // falseSee also
string, int, bool type, Boolean literals, logical operators, type, Random.bool
