Laurel Language Designer Guide

3.2. Truthiness coercion🔗

A value in boolean position — if, while, assert, boolean operators — coerces to bool through a canonical witness: int_to_bool, str_to_bool, list_to_bool, and so on. This coercion applies in boolean position specifically, so it lives in the toBool hook, which fires at boolean-context slots. An int in a condition (if 5) truthifies through int_to_bool; an int in a plain bool slot (var b: bool := 5) is a type error. coerce handles Any in boolean position directly, as the project bool verdict realized by Any_to_bool; toBool supplies the witnesses for the concrete types — int, str, list, and the rest.

In the future that mechanism can be superseded by configurable coercive subtyping, but care needs to be taken to ensure that the resulting relation is coherent. Such a mechanism could also handle other implicit casts — for example, dereferencing a nullable reference in Java (Nullable<T> => T) in a position that expects a T but is provided a Nullable<T>.