\hypertarget{hashdecl_hashdecl_overview}{}\doxysection{Type-\/\+Safe Hashes Overview}\label{hashdecl_hashdecl_overview} The {\ttfamily {\bfseries{hashdecl}}} keyword allows for type-\/safe hashes to be declared; they can then be instantiated with the \mbox{\hyperlink{operators_new}{new}} operator, the \mbox{\hyperlink{operators_cast}{cast$<$$>$}} operator, or with \mbox{\hyperlink{variables_variable_implicit_construction}{variable implicit construction}}.\hypertarget{hashdecl_hashdecl_syntax}{}\doxysection{hashdecl Declaration Syntax}\label{hashdecl_hashdecl_syntax} {\ttfamily {\bfseries{hashdecl}}} {\itshape hashdecl\+\_\+identifier} {\ttfamily \{} ~\newline ~~~~{\itshape \mbox{\hyperlink{data_type_declarations}{member\+\_\+type}}}{\ttfamily }{\itshape member\+\_\+name} {\ttfamily \mbox{[}= }{\itshape \mbox{\hyperlink{expressions}{initialization\+\_\+expression}}}{\ttfamily \mbox{]};} ~\newline ~~~~\mbox{[}...\mbox{]} ~\newline {\ttfamily \}} At least one member must be defined; it\textquotesingle{}s not possible to declare an empty type-\/safe hash. \begin{DoxyParagraph}{Example} \begin{DoxyCode}{0} \DoxyCodeLine{hashdecl MyHash \{} \DoxyCodeLine{ int i = 1;} \DoxyCodeLine{ string code = \textcolor{stringliteral}{"{}other"{}};} \DoxyCodeLine{\}} \end{DoxyCode} \end{DoxyParagraph} \begin{DoxyNote}{Note} \begin{DoxyItemize} \item a {\ttfamily hashdecl} may not have the name {\ttfamily \char`\"{}auto\char`\"{}}, this name has a special meaning in complex types \item Each Qore type has a \char`\"{}pseudo-\/class\char`\"{} associated with it; for hashes the type is \mbox{\hyperlink{class_qore_1_1zzz8hashzzz9}{$<$hash$>$}}; methods from the data type\textquotesingle{}s \char`\"{}pseudo-\/class\char`\"{} can be run on any value of that type. \end{DoxyItemize} \end{DoxyNote} \hypertarget{hashdecl_hashdecl_creation}{}\doxysection{Type-\/\+Safe Hash Creation}\label{hashdecl_hashdecl_creation} When type-\/safe hashes are created, the hash is automatically populated with the values given by the initialization expressions in the {\ttfamily {\bfseries{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\+: \begin{DoxyParagraph}{Examples} \begin{DoxyCode}{0} \DoxyCodeLine{\textcolor{comment}{\# type-\/safe hash declaration}} \DoxyCodeLine{hashdecl Container \{} \DoxyCodeLine{ int i = 1;} \DoxyCodeLine{\}} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# immediate value with implicit construction: default values are assigned from the declaration}} \DoxyCodeLine{auto ah1 = hash\{\};} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# immediate value with implicit construction: default values are assigned from the declaration}} \DoxyCodeLine{auto ah2 = \{\};} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# immediate value with implicit construction: overrides the "{}i"{} member's initial value}} \DoxyCodeLine{auto ah3 = \{\textcolor{stringliteral}{"{}i"{}}: 2\};} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# implicit construction: default values are assigned from the declaration}} \DoxyCodeLine{hash h1();} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# implicit construction: overrides the "{}i"{} member's initial value}} \DoxyCodeLine{hash h2((\textcolor{stringliteral}{"{}i"{}}: 2));} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# "{}new"{} construction: default values are assigned from the declaration}} \DoxyCodeLine{hash h3 = new hash();} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# "{}new"{} construction: overrides the "{}i"{} member's initial value}} \DoxyCodeLine{hash h4 = new hash((\textcolor{stringliteral}{"{}i"{}}: 2));} \end{DoxyCode} \end{DoxyParagraph} In such cases, the initialization expression for the members being overridden is never executed and the supplied value is used instead. Note that it\textquotesingle{}s illegal to assign a value to an unassigned lvalue declared as a typed hash; in such cases a {\ttfamily HASHDECL-\/\+IMPLICIT-\/\+CONSTRUCTION-\/\+ERROR} exception is thrown as in the following example\+: \begin{DoxyCode}{0} \DoxyCodeLine{hash c;} \DoxyCodeLine{\textcolor{comment}{\# the following line will result in a HASHDECL-\/IMPLICIT-\/CONSTRUCTION-\/ERROR exception being thrown}} \DoxyCodeLine{c.i = 2;} \end{DoxyCode} To address this, ensure that your typed hash lvalues are always initialized before assignment as in the following example\+: \begin{DoxyCode}{0} \DoxyCodeLine{hash c();} \DoxyCodeLine{c.i = 2;} \end{DoxyCode} \begin{DoxySeeAlso}{See also} \begin{DoxyItemize} \item \mbox{\hyperlink{variables_variable_implicit_construction}{Variable Implicit Construction}} \item \mbox{\hyperlink{operators_new}{New Value Operator (new)}} \end{DoxyItemize} \end{DoxySeeAlso} \hypertarget{hashdecl_hashdecl_type_compatibility}{}\doxysection{Type-\/\+Safe Hash Type Compatibility}\label{hashdecl_hashdecl_type_compatibility} Type-\/safe hashes can be assigned to any variable that accepts an untyped \mbox{\hyperlink{data_type_declarations_hash_type}{hash}}, however variables declared as a particular type-\/safe hash (ex\+: {\ttfamily hash$<$My\+Hash$>$}) can only be assigned values of the given type; use the \mbox{\hyperlink{operators_cast}{cast$<$$>$}} operator to convert untyped hashes or type-\/safe hashes of another type to the target type for assignment. \begin{DoxyParagraph}{Example\+:} \begin{DoxyCode}{0} \DoxyCodeLine{hashdecl Container1 \{} \DoxyCodeLine{ int i = 1;} \DoxyCodeLine{ string str = \textcolor{stringliteral}{"{}value"{}};} \DoxyCodeLine{\}} \DoxyCodeLine{} \DoxyCodeLine{hashdecl Container2 \{} \DoxyCodeLine{ int i = 2;} \DoxyCodeLine{ bool check = \textcolor{keyword}{False};} \DoxyCodeLine{\}} \DoxyCodeLine{} \DoxyCodeLine{hash c1();} \DoxyCodeLine{} \DoxyCodeLine{\textcolor{comment}{\# afterwards c2 will be: \{i: 1, check: False\}}} \DoxyCodeLine{hash c2 = cast>(cast(c1 -\/ \textcolor{stringliteral}{"{}str"{}}));} \DoxyCodeLine{} \DoxyCodeLine{hash h = (\textcolor{stringliteral}{"{}i"{}}: 3, \textcolor{stringliteral}{"{}other"{}}: \textcolor{stringliteral}{"{}x"{}});} \DoxyCodeLine{\textcolor{comment}{\# afterwards c3 will be: \{i: 3, check: False\}}} \DoxyCodeLine{hash c3 = cast>(cast(h -\/ \textcolor{stringliteral}{"{}other"{}}));} \end{DoxyCode} \end{DoxyParagraph} \begin{DoxySeeAlso}{See also} \mbox{\hyperlink{operators_cast}{cast$<$$>$}} \end{DoxySeeAlso} \hypertarget{hashdecl_hashdecl_object_comparison}{}\doxysection{Comparison of Type-\/\+Safe Hashes and Objects}\label{hashdecl_hashdecl_object_comparison} Type-\/safe hashes are similar to objects in that they have members, but there are some important differences as follows\+: \begin{DoxyItemize} \item type-\/safe hashes are always passed by value whereas objects are always passed by reference \item object classes can have methods, type-\/safe hashes cannot \item object classes allow for class members to have access restrictions; type-\/safe hashes are made up exclusively of public members \item object classes can participate in a hierarchy, type-\/safe hashes cannot \end{DoxyItemize}