uce/site/doc/pages/to_bool.txt

38 lines
1.4 KiB
Plaintext

:sig
bool DValue::to_bool(bool default_value = false) const
:params
default_value : returned when the value is missing or empty
return value : the value as a boolean, or `default_value`
:see
>types
0_DValue
json_decode
to_f64
to_u64
to_string
:content
Reads a `DValue` value as a boolean. This is a read accessor: it is `const`, never creates or modifies nodes, and dereferences internal references automatically.
String values such as `true`, `yes`, `on`, and `1` read as true. Values such as `false`, `no`, `off`, `0`, and `null` read as false. Numeric values read as true when non-zero.
`default_value` is returned when the node is unset, holds an empty string, or is an unresolvable reference. Note the truthiness rule: a non-empty string that parses as neither a boolean word nor a number still reads as `true` — only missing/empty values fall back to the default.
A map-shaped node with exactly one entry unwraps to that entry's value; other maps read as true when non-empty.
## Example
```cpp
bool log_requests = context.cfg.get_by_path("app/log_requests").to_bool(true);
bool wants_compact = context.props["compact"].to_bool();
```
Use this when consuming request data, JSON-decoded values, config trees, or component props where the original input may be string-shaped.
## Related Concepts
- PHP: `filter_var($v, FILTER_VALIDATE_BOOLEAN)`
- JavaScript: `Boolean(value)` plus string handling