Qore Programming Language Reference Manual: Hashdecl: Type-Safe Hash Declarations (original) (raw)

Type-Safe Hashes Overview

The **hashdecl** keyword allows for type-safe hashes to be declared; they can then be instantiated with the new operator, the cast<> operator, or with variable implicit construction.

hashdecl Declaration Syntax

**hashdecl** hashdecl_identifier {
member_type member_name [= initialization_expression];
[...]
}

At least one member must be defined; it's not possible to declare an empty type-safe hash.

Example

hashdecl MyHash {

int i = 1;

string code = "other";

}

Note

Type-Safe Hash Creation

When type-safe hashes are created, the hash is automatically populated with the values given by the initialization expressions in the **hashdecl** declaration (if any).

It is possible to override these declarations by passing a hash to be used for initialization; this can be passed the single optional argument to the type-safe hash initialization as in the following examples:

Examples

hashdecl Container {

int i = 1;

}

auto ah1 = hash{};

auto ah2 = {};

auto ah3 = {"i": 2};

hash h1();

hash h2(("i": 2));

hash h3 = new hash();

hash h4 = new hash(("i": 2));

In such cases, the initialization expression for the members being overridden is never executed and the supplied value is used instead.

Note that it's illegal to assign a value to an unassigned lvalue declared as a typed hash; in such cases a HASHDECL-IMPLICIT-CONSTRUCTION-ERROR exception is thrown as in the following example:

hash c;

c.i = 2;

To address this, ensure that your typed hash lvalues are always initialized before assignment as in the following example:

hash c();

c.i = 2;

See also

Type-Safe Hash Type Compatibility

Type-safe hashes can be assigned to any variable that accepts an untyped hash, however variables declared as a particular type-safe hash (ex: hash<MyHash>) can only be assigned values of the given type; use the cast<> operator to convert untyped hashes or type-safe hashes of another type to the target type for assignment.

Example:

hashdecl Container1 {

int i = 1;

string str = "value";

}

hashdecl Container2 {

int i = 2;

bool check = False;

}

hash c1();

hash c2 = cast<hash>(cast(c1 - "str"));

hash h = ("i": 3, "other": "x");

hash c3 = cast<hash>(cast(h - "other"));

See also

cast<>

Comparison of Type-Safe Hashes and Objects

Type-safe hashes are similar to objects in that they have members, but there are some important differences as follows: