var.init (NULL) if:
- The key does not exist in the object.
- This var is not an object (e.g., NULL, array, scalar).
var get(const string key) const @safeSafely get a field value, returning NULL if missing.
Unlike opIndex or opDispatch, this method does not auto-vivify (create) missing keys or assert on missing keys/non-object types. Instead, it returns var.init (NULL) when the key is missing or the var is not an object.
This enables safe, side-effect-free chained access patterns like: v.get("config").get("database").get("host") which returns NULL if any part of the path is missing, without modifying the original var.
key | The field name to access. |
var.init (NULL) if:
var config;
config["timeout"] = 30;
config["database"]["host"] = "localhost";
// Simple access
assert(config.get("timeout").as!int == 30);
assert(config.get("missing").isNull);
// Chained access - safe even if intermediate keys are missing
assert(config.get("database").get("host").as!string == "localhost");
assert(config.get("database").get("port").isNull);
assert(config.get("nonexistent").get("deep").get("path").isNull);
// Safe on non-objects
var num = 42;
assert(num.get("anything").isNull);var get(TDefault)(const string key, TDefault defaultValue) const @safeGet the value for key or return defaultValue if missing.
Does not modify the map when the key is missing.