uce/site/doc/pages/to_string.txt

41 lines
1.3 KiB
Plaintext

:sig
String DValue::to_string(String default_value = "") const
:params
default_value : returned when the node holds no usable text
return value : the scalar content as text, or `default_value`
:see
>types
0_DValue
to_s64
to_f64
to_bool
to_json
get_by_path
:content
Reads a `DValue` value as text. This is a read accessor: it is `const`, never creates or modifies nodes, and dereferences internal references automatically.
`default_value` is returned when the value is missing or not text-convertible:
- the node is unset or holds an empty string (a missing key read via `get_by_path()` looks exactly like this)
- the node is map-shaped (use `json_encode()` or `to_stringmap()` for those)
- the node is an unresolvable reference
Other scalar kinds convert instead of falling back: `f64` values format with six decimal places (`std::to_string`), `bool` values become `(true)` / `(false)`, and pointers format as their numeric address.
## Example
```cpp
String title = context.props["title"].to_string("Untitled");
String theme = context.cfg.get_by_path("theme/key").to_string("light");
```
The default argument replaces the older `first(x.to_string(), "fallback")` idiom.
## Related Concepts
- PHP: `$value ?? 'fallback'` / `strval()`
- JavaScript: `String(value || "fallback")`