Merge another object into this one.
This method combines the key-value pairs from other into this var. It only works when both this and other are objects (Type.OBJECT).
Conflict resolution:
- By default, values from
other overwrite existing values in this (last wins). - If
deepMerge is true, nested objects are recursively merged instead of replaced. Parameters
other | The object to merge into this one. |
deepMerge | If true, recursively merge nested objects. Default is false. |
Returns
A reference to
this for method chaining.
- Preconditions:
- Both
this and other must be Type.OBJECT (or this is NULL, which gets promoted). - Throws a
VarTypeException if either is a non-object, non-NULL type. Throws
VarException when deep merging recurses beyond the structural depth- limit (cycles created through legal shallow aliasing). Key/value pairs
- merged before the error are retained (partial update).
Examples
---
- var base;
- base["a"] = 1;
- base["b"] = 2;
- var overlay;
- overlay["b"] = 20;
- overlay["c"] = 30;
- base.merge(overlay);
- assert(base["a"].as!int == 1);
- assert(base["b"].as!int == 20); // overwritten
- assert(base["c"].as!int == 30); // added
- ---