defaultValue if not found or not an object.var.getField
var getField(T)(const string key, T defaultValue) const @safeSafely 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
key | The field name to access. |
defaultValue | The value to return if the field is missing or this is not an object. |
Returns
The value at the given field, or
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);