var.getField

var getField(T)(const string key, T defaultValue) const @safe

Safely get a field value with a default fallback.

Unlike opDispatch, this method does not assert on missing keys or non-object types. Instead, it returns the provided default value.

This is useful when you want to access a field that may or may not exist, without risking an assertion failure.

Parameters

keyThe field name to access.
defaultValueThe value to return if the field is missing or this is not an object.

Returns

The value at the given field, or defaultValue if not found or not an object.

Examples

var config;
config["timeout"] = 30;
assert(config.getField("timeout", 10).as!int == 30);
assert(config.getField("missing", 10).as!int == 10);
var notAnObject = 42;
assert(notAnObject.getField("x", 99).as!int == 99);