ddn.var

Types 5

classVarException : Exception

Base exception type for ddn.var runtime misuse.

Constructors
this(string msg, string file = __FILE__, size_t line = __LINE__)

Thrown when an operation requires a specific runtime var.Type.

Constructors
this(string op, var.Type expected, var.Type actual, string details = null, string file = __FILE__, size_t line = __LINE__)

Thrown when an object key is missing for an operation that requires it.

Constructors
this(string op, string key, string file = __FILE__, size_t line = __LINE__)

Thrown when an array index is out of range for an operation that requires it.

Constructors
this(string op, size_t index, size_t length, string file = __FILE__, size_t line = __LINE__)
structvar

ddn.var — a compact, dynamic value type.

Error handling strategy:

  • By default, wrong-type use of container APIs throws exceptions (e.g. indexing a non-object or using array APIs on non-arrays). This is enforced in both debug and -release builds.
  • If compiled with version(DDN_VAR_TYPE_COERCE), var becomes permissive:
  • Mutating/ref-returning array/object APIs will auto-coerce the value to the required container type (overwriting the previous value).
  • Read-only accessors return neutral values on mismatch (no hidden mutation).
  • In debug builds, coercions emit warnings on stderr.
  • No I/O is performed inside this module except the documented debug-only coercion warnings on stderr under version(DDN_VAR_TYPE_COERCE); formatting is provided by toString().

Thread-safety notes:

  • var itself does not perform any internal synchronization. Concurrent mutation of the same instance from multiple threads is a data race and is not supported.
  • Independent copies may be used in different threads; however, keep in mind that string is immutable (safe to share) while arrays/maps inside var are reference-backed containers owned by each var instance.

    Note

    copying/assignment of var values is shallow for Type.ARRAY and Type.OBJECT (it aliases the underlying containers). Use dup() / idup() when you need an independent deep copy.
  • Recommended patterns: message passing of var values; publishing immutable(var) snapshots; or guarding shared access with user-provided synchronization (e.g., a Mutex).
Methods
var opAssign(T)(scope T rhs) @safeAssign from an arbitrary value `rhs`, setting the internal type and payload accordingly.
var opIndex(string key) ref @safeAccess a child value by key, turning this instance into a map on first use.
const(var) opIndex(string key) ref const @safeConst overload of `opIndex` for read-only map access.
void opIndexAssign(T)(T rhs, string key) @safeAssign a value to a child by key (map-style assignment).
void opIndexAssign(T)(T rhs, size_t index) @safeAssign a value to an array element by index.
bool remove(string key) @safeRemove a child by key from the map.
void clear() @safeReset this `var` to `Type.NULL`, releasing any held resources.
var dup() scope const @safeCreate a deep copy of this `var`.
var idup() const @safeCreate an independent deep copy of this `var` under an `idup`-style name.
var merge(const var other, bool deepMerge = false) ref return @safe 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 resol...
var opDispatch(string name)() ref @safeDynamic field access for map values via D's `opDispatch`.
const(var) opDispatch(string name)() ref const @safeConst overload of `opDispatch` for read-only access (no insertion).
var getField(T)(const string key, T defaultValue) const @safeSafely get a field value with a default fallback.
var get(const string key) const @safeSafely get a field value, returning NULL if missing.
var path(const string[] pathParts) const @safeAccess a nested value using an array of path components.
var path(const string pathStr) const @safeAccess a nested value using dot-notation or slash-notation path string.
ElementRange byElement() @safeGet a forward range over the elements of this array.
ConstElementRange byElement() scope const @safeConst overload: get a forward range over the elements of this array.
KeyRange byKey() @safeGet a forward range over the keys of this object.
ValueRange byValue() @safeGet a forward range over the values of this object.
PairRange byPair() @safeGet a forward range over the key-value pairs of this object.
string toStringRoundTrip() const @safeShortest text that parses back to the exact stored floating-point value.
string toString() const @safeConvert the stored value to a human-readable string.
void toStringTo(Sink)(ref Sink sink) if (is(typeof(sink.put("test"))) || is(typeof(sink.put('c')))) const @safeWrite the string representation of this value to an output range.
T as(T)() const @safe
Nullable!T tryAs(T)() const @safe Attempt to convert the stored value to type `T`, returning a `Nullable!T`. `as!T` reports failures two different ways — wrong-kind container mismatches generally return `T.init` (indistinguis...
T opCall(T)() const @safeConvenience call operator that forwards to `as!T()`.
var opIndex(size_t index) ref @safeIndex into the array payload and return a reference to the element.
const(var) opIndex(size_t index) ref const @safeConst overload of `opIndex` for read-only array access.
size_t opDollar() @property const @safeSupport `$` in slice expressions for arrays.
var opSlice(size_t start, size_t end) const @safeSlice an array to create a new `var` containing a subrange of elements.
size_t length() @property const @safeGet the number of elements when the value holds an array.
bool isNativeArray() @property scope const nothrow @nogc pure @safeTrue when this value is a compact native-array value (`typedArray`).
bool isNativeArrayOf(E)() if (_nativeKindOf!E != ElementType.NONE) scope const nothrow @nogc pure @safeTrue when this value is a native array of element type `E`.
bool isUuid() @property const nothrow @nogc pure @safeTrue when this value is a UUID payload (`std.uuid.UUID`).
bool isFixedDecimal() @property const nothrow @nogc pure @safeTrue when this value is a fixed-point decimal (`FIXED_DECIMAL`).
var element(size_t index) const @safeBoxes a single element of a native array into a `var`.
E[] nativeSlice(E)() if (_nativeKindOf!E != ElementType.NONE) @safe Returns the shared mutable slice of a native array. Only valid when the stored buffer carries the mutable qualifier: `typedArray` from a mutable slice, or `dup()`/`idup()` of a mutable-qualifi...
void append(T)(T rhs) @safeAppend a value to the array. Converts `Type.NULL` to an empty array on first use.
void extend(T)(T rhs) @safeExtend this array with elements from another array (flat concatenation).
void opOpAssign(string op, T)(T rhs) if (op == "~" || op == "+" || op == "-" || op == "*" || op == "/" || op == "%") @safeOperator `~=` for append or string concatenation.
bool contains(const string key) const @safeCheck whether the map contains `key`.
var get(TDefault)(const string key, TDefault defaultValue) const @safeGet the value for `key` or return `defaultValue` if missing.
string[] keys() @property const @safeReturn an array of keys. For `Type.NULL`, returns an empty array.
var[] values() @property const @safeReturn an array of values. For `Type.NULL`, returns an empty array.
size_t count() @property const @safeNumber of key/value pairs stored. Returns 0 for `Type.NULL`.
int opApply(scope int delegate(const string, ref var) dg)Iterate over map entries as `(key, ref value)` pairs. For non-map types, iteration yields zero elements.
int opApply(scope int delegate(const string, ref var) @safe dg) @safe@safe counterpart of opApply for @safe loop bodies.
int opApply(scope int delegate(const string, ref const(var)) @safe dg) const @safeConst iteration over map entries as `(key, ref const value)` pairs.
bool insert(T)(size_t index, T rhs) @safeInsert `rhs` at position `index` (0..length). Converts `Type.NULL` to empty array. Returns `true` on success, `false` if index is out of range.
bool removeAt(size_t index) @safeRemove the element at `index`. Returns `true` if removed, `false` if `index` is out of range or the value is `Type.NULL`. Throws a `VarTypeException` if the current type is neither `NULL` nor `ARRAY`.
int opApply(scope int delegate(ref var) dg)Foreach iteration support over array elements.
int opApply(scope int delegate(ref var) @safe dg) @safe@safe counterpart for @safe loop bodies.
int opApply(scope int delegate(ref const(var)) @safe dg) const @safeConst foreach iteration over array elements.
int opApply(scope int delegate(size_t, ref var) dg)Foreach iteration with index over array elements.
int opApply(scope int delegate(size_t, ref var) @safe dg) @safe@safe counterpart for @safe loop bodies.
int opApply(scope int delegate(size_t, ref const(var)) @safe dg) const @safeConst foreach iteration with index over array elements.
bool opEquals(const scope var rhs) const @trustedStructural equality.
var opBinary(string op)(const var rhs) if (op == "+" || op == "-" || op == "*" || op == "/" || op == "%") const @safe Binary arithmetic operators for `var`. Supported operators: `+`, `-`, ``, `/`, `%` Type promotion rules: - If either operand is `DOUBLE`, `FLOAT`, or `REAL`, the result is `DOUBLE`. - If eit...
var opBinary(string op)(const var rhs) if (op == "~") const @safeString concatenation operator for `var`.
var opUnary(string op)() if (op == "-" || op == "!") const @safeUnary operators for `var`.
int opCmp(const var rhs) const @safe Three-way comparison operator for `var`. Enables use of `<`, `<=`, `>`, `>=` operators between `var` values. Comparison rules: - *Numeric types: Compared by numeric value with automatic type ...
size_t toHash() const @safeCompute a structural hash value consistent with `opEquals`.
Type type() @property scope const nothrow @nogc pure @safeGet the current type tag of this value.
bool isNull() @property const nothrow @nogc pure @safeCheck if this value is null (Type.NULL).
bool isArray() @property scope const nothrow @nogc pure @safeCheck if this value is an array (Type.ARRAY).
bool isObject() @property const nothrow @nogc pure @safeCheck if this value is an object/map (Type.OBJECT).
bool isNumeric() @property const nothrow @nogc pure @safeCheck if this value is a numeric type.
bool isInteger() @property const nothrow @nogc pure @safeReturns true if this is any integer type (BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG).
bool isFloat() @property const nothrow @nogc pure @safeReturns true if this is any floating-point type (FLOAT, DOUBLE, REAL).
bool isString() @property const nothrow @nogc pure @safeCheck if this value is a string (Type.STRING).
bool empty() @property const @safeCheck if this value is empty.
bool isBool() @property const nothrow @nogc pure @safeCheck if this value is a boolean (Type.BOOL).
bool isDate() @property const nothrow @nogc pure @safeCheck if this `var` holds a `Date` value.
bool isTime() @property const nothrow @nogc pure @safeCheck if this `var` holds a `TimeOfDay` value.
bool isDateTime() @property const nothrow @nogc pure @safeCheck if this `var` holds a `DateTime` value.
bool isSysTime() @property const nothrow @nogc pure @safeCheck if this `var` holds a `SysTime` value.
bool isTimestamp() @property const nothrow @nogc pure @safeCheck if this `var` holds a `ddn.datetime.Timestamp` value.
bool isDuration() @property const nothrow @nogc pure @safeCheck if this `var` holds a `Duration` value.
bool isTemporal() @property const nothrow @nogc pure @safeCheck if this `var` holds any temporal (datetime) type.
auto visit(handlers...)() scope const @safeApply a visitor pattern to this value, dispatching to the appropriate handler based on type.
auto visitNogc(handlers...)() const @safe @nogc`visitNogc` is a no-GC variant of `visit`.
var makeArray() static @safeCreate an empty array value (`Type.ARRAY`).
var makeObject() static @safeCreate an empty map value (`Type.OBJECT`).
var emptyArray() static @property @safeReturns an empty array value (`Type.ARRAY`).
var emptyObject() static @property @safeReturns an empty object/map value (`Type.OBJECT`).
var array_() static @property @safeAlias for `emptyArray`. Returns an empty array value (`Type.ARRAY`).
var object_() static @property @safeAlias for `emptyObject`. Returns an empty object/map value (`Type.OBJECT`).
var fromArray(T)(T[] arr) static @safeConstruct a `var` from an array `arr`, converting elements to `var`.
var fromMap(const var[string] m) static @safeConstruct a `var` from a map `m` (string -> var).
Constructors
this(ref return scope inout var rhs) Copy constructor for same-constness copies (mutable→mutable, const→const, immutable→immutable, and compiler-generated `inout` copies in structs that embed `var`): shallow payload copy, mat...
this(ref return scope const var rhs)Copy constructor from a `const` `var` into a mutable one: deep copy. A shallow blit would hand back a mutable alias to the const payload, letting @safe callers mutate "const" data (and race on shar...
this(ref return scope immutable var rhs)Copy constructor from an `immutable` `var` into a mutable one: deep copy (see the `const` overload).
this(ref return scope const var rhs)Copy constructor from a `const` `var` into a `const` one: shallow copy. A const destination cannot be used to mutate the payload, so aliasing is sound.
this(ref return scope immutable var rhs)Copy constructor from an `immutable` `var` into a `const` one: shallow copy (a const view of immutable data; nobody can mutate it).
this(T arg)Construct a `var` from another value `arg`.
Nested Templates
TypeThe concrete type of a `var` value.
ElementTypeElement kind of a native-array value (`Type.NATIVE_ARRAY`).
NativeQualElement qualifier recorded at native-array construction. Controls whether a mutable view may be handed out; const views are always safe.
DecimalCapFixed-decimal capacity of a `Type.FIXED_DECIMAL` value.
_isFDInst(T)True for any `FixedDecimal` instantiation.
_nativeKindOf(T)Maps a (possibly const/immutable-qualified) element type to its kind.
_isSharedQualified(T)True when `T` carries the `shared` qualifier (including shared-const forms).
_isNativeSlice(T)True for dynamic arrays of whitelisted native element types.
_isNativeView(T)True for `const(E)[]` views over whitelisted native element types.
_nativeViewElem(T)Element type of a native view type `const(E)[]`.
_isBareNativeSlice(T)True for unqualified `E[]` slices of whitelisted native element types.
ElementRangeA forward range over the elements of a `var` array.
ConstElementRangeA forward range over the elements of a `const var` array.
KeyRangeA forward range over the keys of a `var` object.
ValueRangeA forward range over the values of a `var` object.
KeyValuePairA key-value pair returned by `PairRange`.
PairRangeA forward range over the key-value pairs of a `var` object.
_PutAdapterNormalizes a sink that provides either put capability to one that provides both.