\doxysection{Hashdecl\+: Type-\/\+Safe Hash Declarations}
\hypertarget{hashdecl}{}\label{hashdecl}\index{Hashdecl: Type-\/Safe Hash Declarations@{Hashdecl: Type-\/Safe Hash Declarations}}
\hypertarget{hashdecl_hashdecl_overview}{}\doxysubsection{\texorpdfstring{Type-\/\+Safe Hashes Overview}{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 \doxysectlink{operators_new}{new}{2} operator, the \doxysectlink{operators_cast}{cast\texorpdfstring{$<$}{<}\texorpdfstring{$>$}{>}}{2} operator, or with \doxysectlink{variables_variable_implicit_construction}{variable implicit construction}{2}.\hypertarget{hashdecl_hashdecl_syntax}{}\doxysubsection{\texorpdfstring{hashdecl Declaration Syntax}{hashdecl Declaration Syntax}}\label{hashdecl_hashdecl_syntax}
{\ttfamily {\bfseries{hashdecl}}} {\itshape hashdecl\+\_\+identifier} {\ttfamily \{} ~\newline
 ~~~~{\itshape \doxylink{data_type_declarations}{member\+\_\+type}}{\ttfamily  }{\itshape member\+\_\+name} {\ttfamily \mbox{[}= }{\itshape \doxylink{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 "{}auto"{}}, this name has a special meaning in complex types
\item Each Qore type has a "{}pseudo-\/class"{} associated with it; for hashes the type is \doxylink{class_qore_1_1zzz8hashzzz9}{\texorpdfstring{$<$}{<}hash\texorpdfstring{$>$}{>}}; methods from the data type\textquotesingle{}s "{}pseudo-\/class"{} can be run on any value of that type.
\end{DoxyItemize}
\end{DoxyNote}
\hypertarget{hashdecl_hashdecl_creation}{}\doxysubsection{\texorpdfstring{Type-\/\+Safe Hash Creation}{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<Container>\{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{\#\ immediate\ value\ with\ implicit\ construction:\ default\ values\ are\ assigned\ from\ the\ declaration}}
\DoxyCodeLine{auto\ ah2\ =\ <Container>\{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{\#\ immediate\ value\ with\ implicit\ construction:\ overrides\ the\ "{}i"{}\ member's\ initial\ value}}
\DoxyCodeLine{auto\ ah3\ =\ <Container>\{\textcolor{stringliteral}{"{}i"{}}:\ 2\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{\#\ implicit\ construction:\ default\ values\ are\ assigned\ from\ the\ declaration}}
\DoxyCodeLine{hash<Container>\ h1();}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{\#\ implicit\ construction:\ overrides\ the\ "{}i"{}\ member's\ initial\ value}}
\DoxyCodeLine{hash<Container>\ h2((\textcolor{stringliteral}{"{}i"{}}:\ 2));}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{\#\ "{}new"{}\ construction:\ default\ values\ are\ assigned\ from\ the\ declaration}}
\DoxyCodeLine{hash<Container>\ h3\ =\ new\ hash<Container>();}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{\#\ "{}new"{}\ construction:\ overrides\ the\ "{}i"{}\ member's\ initial\ value}}
\DoxyCodeLine{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 HASHDECL-\/\+IMPLICIT-\/\+CONSTRUCTION-\/\+ERROR} exception is thrown as in the following example\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{hash<Container>\ 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<Container>\ c();}
\DoxyCodeLine{c.i\ =\ 2;}

\end{DoxyCode}


\begin{DoxySeeAlso}{See also}

\begin{DoxyItemize}
\item \doxysectlink{variables_variable_implicit_construction}{Variable Implicit Construction}{2}
\item \doxysectlink{operators_new}{New Value Operator (new)}{2}
\end{DoxyItemize}
\end{DoxySeeAlso}
\hypertarget{hashdecl_hashdecl_type_compatibility}{}\doxysubsection{\texorpdfstring{Type-\/\+Safe Hash Type Compatibility}{Type-Safe Hash Type Compatibility}}\label{hashdecl_hashdecl_type_compatibility}
Type-\/safe hashes can be assigned to any variable that accepts an untyped \doxysectlink{data_type_declarations_hash_type}{hash}{1}, however variables declared as a particular type-\/safe hash (ex\+: {\ttfamily hash\texorpdfstring{$<$}{<}My\+Hash\texorpdfstring{$>$}{>}}) can only be assigned values of the given type; use the \doxysectlink{operators_cast}{cast\texorpdfstring{$<$}{<}\texorpdfstring{$>$}{>}}{2} 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<Container1>\ c1();}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{comment}{\#\ afterwards\ c2\ will\ be:\ \{i:\ 1,\ check:\ False\}}}
\DoxyCodeLine{hash<Container2>\ c2\ =\ cast<hash<Container2>>(cast<hash>(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<Container2>\ c3\ =\ cast<hash<Container2>>(cast<hash>(h\ -\/\ \textcolor{stringliteral}{"{}other"{}}));}

\end{DoxyCode}

\end{DoxyParagraph}
\begin{DoxySeeAlso}{See also}
\doxysectlink{operators_cast}{cast\texorpdfstring{$<$}{<}\texorpdfstring{$>$}{>}}{2}
\end{DoxySeeAlso}
\hypertarget{hashdecl_hashdecl_object_comparison}{}\doxysubsection{\texorpdfstring{Comparison of Type-\/\+Safe Hashes and Objects}{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}