Qore Programming Language Reference Manual
1.12.1
|
References allow for addressing other lvalues, including complex lvalue expressions, with an alias called a reference. References to lvalues are defined by placing a \
character in front of an expression that gives an lvalue, such as in the following examples:
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 "order"
key - in the following example, we want to set the last record's last order's "numberofitems"
key to be equal to the number of elements under the order's "items"
key):
In the above code, using references could greatly simplify the readability of the code:
The following reference type restrictions exist:
Once assigned, a reference is permanently assigned to the lvalue; there is currently no way to "unreference" a reference.
The *reference 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 "*reference" type to be used even without a reference assigned.
Consider the following example:
The above code is valid and will work as described in the comments above.