![]() |
Qore Programming Language Reference Manual
0.9.3.2
|
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 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.
hashdecl may not have the name "auto", this name has a special meaning in complex typesWhen 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:
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:
To address this, ensure that your typed hash lvalues are always initialized before assignment as in the following example:
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.
Type-safe hashes are similar to objects in that they have members, but there are some important differences as follows: