\hypertarget{hashdecl_hashdecl_overview}{}\section{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 \hyperlink{operators_new}{new} operator, the \hyperlink{operators_cast}{cast$<$$>$} operator, or with \hyperlink{variables_variable_implicit_construction}{variable implicit construction}.\hypertarget{hashdecl_hashdecl_syntax}{}\section{hashdecl Declaration Syntax}\label{hashdecl_hashdecl_syntax}
{\ttfamily {\bfseries hashdecl}} {\itshape hashdecl\+\_\+identifier} {\ttfamily \{} ~\newline
 ~~~~{\itshape \hyperlink{data_type_declarations}{member\+\_\+type}}{\ttfamily  }{\itshape member\+\_\+name} {\ttfamily \mbox{[}= }{\itshape \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}
hashdecl MyHash \{
    int i = 1;
    string code = \textcolor{stringliteral}{"other"};
\}
\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 \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}{}\section{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}
\textcolor{comment}{# type-safe hash declaration}
hashdecl Container \{
    int i = 1;
\}

\textcolor{comment}{# immediate value with implicit construction: default values are assigned from the declaration}
auto ah1 = hash<Container>\{\};

\textcolor{comment}{# immediate value with implicit construction: default values are assigned from the declaration}
auto ah2 = <Container>\{\};

\textcolor{comment}{# immediate value with implicit construction: overrides the "i" member's initial value}
auto ah3 = <Container>\{\textcolor{stringliteral}{"i"}: 2\};

\textcolor{comment}{# implicit construction: default values are assigned from the declaration}
hash<Container> h1();

\textcolor{comment}{# implicit construction: overrides the "i" member's initial value}
hash<Container> h2((\textcolor{stringliteral}{"i"}: 2));

\textcolor{comment}{# "new" construction: default values are assigned from the declaration}
hash<Container> h3 = new hash<Container>();

\textcolor{comment}{# "new" construction: overrides the "i" member's initial value}
hash<Container> h4 = new hash<Container>((\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 H\+A\+S\+H\+D\+E\+C\+L-\/\+I\+M\+P\+L\+I\+C\+I\+T-\/\+C\+O\+N\+S\+T\+R\+U\+C\+T\+I\+O\+N-\/\+E\+R\+R\+OR} exception is thrown as in the following example\+: 
\begin{DoxyCode}
hash<Container> c;
\textcolor{comment}{# the following line will result in a HASHDECL-IMPLICIT-CONSTRUCTION-ERROR exception being thrown}
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}
hash<Container> c();
c.i = 2;
\end{DoxyCode}


\begin{DoxySeeAlso}{See also}

\begin{DoxyItemize}
\item \hyperlink{variables_variable_implicit_construction}{Variable Implicit Construction}
\item \hyperlink{operators_new}{New Value Operator (new)}
\end{DoxyItemize}
\end{DoxySeeAlso}
\hypertarget{hashdecl_hashdecl_type_compatibility}{}\section{Type-\/\+Safe Hash Type Compatibility}\label{hashdecl_hashdecl_type_compatibility}
Type-\/safe hashes can be assigned to any variable that accepts an untyped \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 \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}
hashdecl Container1 \{
    int i = 1;
    string str = \textcolor{stringliteral}{"value"};
\}

hashdecl Container2 \{
    int i = 2;
    bool check = \textcolor{keyword}{False};
\}

hash<Container1> c1();

\textcolor{comment}{# afterwards c2 will be: \{i: 1, check: False\}}
hash<Container2> c2 = cast<hash<Container2>>(cast<hash>(c1 - \textcolor{stringliteral}{"str"}));

hash h = (\textcolor{stringliteral}{"i"}: 3, \textcolor{stringliteral}{"other"}: \textcolor{stringliteral}{"x"});
\textcolor{comment}{# afterwards c3 will be: \{i: 3, check: False\}}
hash<Container2> c3 = cast<hash<Container2>>(cast<hash>(h - \textcolor{stringliteral}{"other"}));
\end{DoxyCode}

\end{DoxyParagraph}
\begin{DoxySeeAlso}{See also}
\hyperlink{operators_cast}{cast$<$$>$}
\end{DoxySeeAlso}
\hypertarget{hashdecl_hashdecl_object_comparison}{}\section{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}