\doxysection{References}
\hypertarget{lvalue_references}{}\label{lvalue_references}\index{References@{References}}
\hypertarget{lvalue_references_references_overview}{}\doxysubsection{\texorpdfstring{Overview of References}{Overview of References}}\label{lvalue_references_references_overview}
References allow for addressing other lvalues, including complex lvalue expressions, with an alias called a reference. References to lvalues are defined by placing a {\ttfamily \textbackslash{}} character in front of an expression that gives an lvalue, such as in the following examples\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{reference\ r1\ =\ \(\backslash\)a;}
\DoxyCodeLine{reference\ r2\ =\ \(\backslash\)recs[rn].order[on];}

\end{DoxyCode}


References are especially convenient when aliasing an internal part of a complex data structure. Consider the following example (a list of records, where each record is a hash with a list of orders under the {\ttfamily "{}order"{}} key -\/ in the following example, we want to set the last record\textquotesingle{}s last order\textquotesingle{}s {\ttfamily "{}numberofitems"{}} key to be equal to the number of elements under the order\textquotesingle{}s {\ttfamily "{}items"{}} key)\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{recs[recs.size()\ -\/\ 1].order[recs[recs.size()\ -\/\ 1].order.size()\ -\/\ 1].numberofitems\ =\ recs[recs.size()\ -\/\ 1].order[recs[recs.size()\ -\/\ 1].order.size()\ -\/\ 1].items.size();}

\end{DoxyCode}


In the above code, using references could greatly simplify the readability of the code\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{reference\ lastrec\ =\ \(\backslash\)recs[recs.size()\ -\/\ 1];}
\DoxyCodeLine{reference\ lastorder\ =\ \(\backslash\)lastrec.order[lastrec.order.size()\ -\/\ 1];}
\DoxyCodeLine{lastorder.numberofitems\ =\ lastorder.items.size();}

\end{DoxyCode}


\begin{DoxyNote}{Note}
Local variables that are referenced are automatically converted to a special type of local variable that is protected by a mutual-\/exclusion thread lock so that the reference can be safely used in background threads. The lvalue represented by the local variable will exist beyond its local scope; as long as the reference to the lvalue exists, the local variable\textquotesingle{}s lvalue will exist and remain valid.
\end{DoxyNote}
\begin{DoxySince}{Since}
Qore 0.\+8.\+5 universal lvalue references are supported; previously references could only be used with arguments to a function or method call or the like. As of Qore 0.\+8.\+5+, references can be used anywhere in Qore in any expression, even with references to local variables in background expressions and in closure argument lists.
\end{DoxySince}
\hypertarget{lvalue_references_reference_types}{}\doxysubsection{\texorpdfstring{Reference Type Restrictions}{Reference Type Restrictions}}\label{lvalue_references_reference_types}
The following reference type restrictions exist\+:
\begin{DoxyItemize}
\item \doxysectlink{data_type_declarations_reference_type}{reference}{1}\+: requires a reference when assigned
\item \doxysectlink{data_type_declarations_reference_or_nothing_type}{\texorpdfstring{$\ast$}{*}reference}{1}\+: requires either a reference or \doxysectlink{basic_data_types_nothing}{NOTHING}{1}
\end{DoxyItemize}

Once assigned, a reference is permanently assigned to the lvalue; there is currently no way to "{}unreference"{} a reference.

The \doxysectlink{data_type_declarations_reference_or_nothing_type}{\texorpdfstring{$\ast$}{*}reference}{1} has special behavior when used as an argument type restriction. In this case, if no value is assigned to the reference in the call, the local variable can be assigned to any value in the code block, even if the called did not pass a value to the reference. This allows variables of "{}\texorpdfstring{$\ast$}{*}reference"{} type to be used even without a reference assigned.

Consider the following example\+: 
\begin{DoxyCode}{0}
\DoxyCodeLine{sub\ ref(*reference\ r)\ \{}
\DoxyCodeLine{\ \ \ \ r\ =\ \{\};}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{hash\ h;}
\DoxyCodeLine{\textcolor{comment}{\#\ in\ this\ case,\ the\ reference\ in\ "{}ref()"{}\ updates\ the\ local\ variable\ in\ "{}ref()"{}}}
\DoxyCodeLine{ref();}
\DoxyCodeLine{\textcolor{comment}{\#\ in\ this\ case,\ the\ reference\ updates\ top-\/level\ local\ variable\ "{}h"{}}}
\DoxyCodeLine{ref(\(\backslash\)h);}

\end{DoxyCode}


The above code is valid and will work as described in the comments above. 