ddn.var¶
A compact, dynamic value type for the D programming language.
Overview¶
This module provides var, a versatile dynamic value type that can hold values of many different types at runtime. It is designed to be compact, efficient, and easy to use for scenarios where static typing is impractical—such as configuration handling, JSON-like data structures, or scripting interfaces.
Structs¶
var¶
A compact, dynamic value type capable of holding scalar values, strings, arrays, and associative arrays.
Error Handling Strategy:
- This library does not throw exceptions for runtime misuse. Instead, programmer errors (e.g., indexing a non-map or using array APIs on non-arrays) are guarded with
assertin debug builds. - Recoverable situations return a status value and do not throw.
- Division/modulo by zero returns
var.init(NULL) instead of throwing.
Thread Safety:
vardoes not perform internal synchronization. Concurrent mutation of the same instance from multiple threads is a data race.- Independent copies may be used in different threads.
- Use
dup()for deep copies when sharing data between threads.
Enums¶
var.Type¶
The concrete type tag for a var value.
| Member | Description |
|---|---|
NULL |
The absence of a value |
BOOL |
Boolean value (true or false) |
BYTE |
Signed 8-bit integer |
UBYTE |
Unsigned 8-bit integer |
SHORT |
Signed 16-bit integer |
USHORT |
Unsigned 16-bit integer |
INT |
Signed 32-bit integer |
UINT |
Unsigned 32-bit integer |
LONG |
Signed 64-bit integer |
ULONG |
Unsigned 64-bit integer |
FLOAT |
32-bit IEEE-754 floating point |
DOUBLE |
64-bit IEEE-754 floating point |
REAL |
Platform real (stored internally as double) |
CHAR |
Narrow character (char) |
WCHAR |
Wide character (wchar) |
DCHAR |
Unicode scalar character (dchar) |
ARRAY |
Dynamic array of var |
STRING |
UTF-8 string |
OBJECT |
Associative array (string → var) |
VAR |
Backward-compatibility alias for OBJECT |
Constructors¶
this(T)¶
Constructs a var from a value of type T.
Parameters:
| Name | Type | Description |
|---|---|---|
arg |
T |
The value to store |
Example:
var a = 42; // INT
var b = "hello"; // STRING
var c = 3.14; // DOUBLE
var d = true; // BOOL
var e = [1, 2, 3]; // ARRAY
Properties¶
type¶
Returns the current type tag of the stored value.
Returns: The Type enum value indicating the stored type.
isNull¶
Returns true if the value is NULL.
isArray¶
Returns true if the value is an ARRAY.
isObject¶
Returns true if the value is an OBJECT.
isNumeric¶
Returns true if the value is a numeric type (integer or floating point).
isString¶
Returns true if the value is a STRING.
isBool¶
Returns true if the value is a BOOL.
length¶
Returns the length of the stored value.
- For
STRING: returns the string length. - For
ARRAY: returns the number of elements. - For
OBJECT: returns the number of key-value pairs. - For other types: returns
0.
keys¶
Returns an array of all keys in an OBJECT. Returns an empty array for non-object types.
values¶
Returns an array of all values in an OBJECT. Returns an empty array for non-object types.
count¶
Returns the number of elements in an ARRAY or key-value pairs in an OBJECT. Returns 0 for other types.
opDollar¶
Returns the length for use with the $ operator in slice expressions.
Methods¶
opAssign(T)¶
Assigns a value of type T to this var.
Parameters:
| Name | Type | Description |
|---|---|---|
rhs |
T |
The value to assign |
Returns: Reference to this.
Note: Assignment from another var is shallow for arrays and objects. Use dup() for deep copies.
as(T)¶
Converts the stored value to type T.
Returns: The value converted to type T, or T.init if conversion is not possible.
Example:
var v = 42;
int i = v.as!int; // 42
string s = v.as!string; // "42"
var arr = [1, 2, 3];
var[] elements = arr.as!(var[]);
dup¶
Creates a deep copy of the var value. For arrays and objects, recursively duplicates all nested elements.
Returns: A new var that is an independent deep copy.
Example:
var original;
original["nested"] = [1, 2, 3];
var copy = original.dup;
copy["nested"][0] = 99;
assert(original["nested"][0].as!int == 1); // Original unchanged
idup¶
Creates an immutable deep copy of the var value.
Returns: A new var with immutable string data.
get(key, defaultValue)¶
Retrieves a value from an OBJECT by key, returning a default if the key is not found.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string |
The key to look up |
defaultValue |
TDefault |
Value to return if key is not found |
Returns: The value associated with key, or defaultValue if not found.
contains¶
Checks if an OBJECT contains the specified key.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string |
The key to check |
Returns: true if the key exists, false otherwise.
remove¶
Removes a key-value pair from an OBJECT.
Parameters:
| Name | Type | Description |
|---|---|---|
key |
string |
The key to remove |
Returns: true if the key was found and removed, false otherwise.
insert¶
Inserts a value into an ARRAY at the specified index.
Parameters:
| Name | Type | Description |
|---|---|---|
index |
size_t |
Position to insert at |
rhs |
T |
Value to insert |
Returns: true if insertion succeeded, false otherwise.
removeAt¶
Removes an element from an ARRAY at the specified index.
Parameters:
| Name | Type | Description |
|---|---|---|
index |
size_t |
Position to remove |
Returns: true if removal succeeded, false otherwise.
clear¶
Resets the var to NULL, releasing any stored value.
toString¶
Converts the var to a human-readable string representation.
Returns: A string representation of the value.
toJSON¶
Serializes the var to a JSON string.
Returns: A JSON-formatted string.
Example:
var data;
data["name"] = "example";
data["values"] = [1, 2, 3];
string json = data.toJSON();
// Output: {"name":"example","values":[1,2,3]}
Operators¶
opIndex (string key)¶
Accesses or creates a value in an OBJECT by key. If the var is NULL, it is promoted to an OBJECT.
Example:
opIndex (size_t index)¶
Accesses an element in an ARRAY by index.
opIndexAssign¶
Assigns a value to an OBJECT key.
opDispatch¶
Provides field-like access to OBJECT keys.
Example:
var obj;
obj.name = "Alice"; // Same as obj["name"] = "Alice"
obj.age = 30;
string name = obj.name.as!string;
opBinary¶
Performs arithmetic operations (+, -, *, /, %) between var values.
Note: Division or modulo by zero returns var.init (NULL).
Example:
opUnary¶
Performs unary operations (-, +, ~, !) on a var value.
opEquals¶
Compares two var values for equality.
opCmp¶
Compares two var values for ordering.
Returns: Negative if this < rhs, zero if equal, positive if this > rhs.
See Also¶
ddn.adam— Memory-efficient alternative tovar.ddn.data.json5— JSON5 parsing and serialization.ddn.util.json_escape— String escaping utilities.