LocalAlias..Do
Requires Analytica 4.2
LocalAlias «x» := «expr» Do «body»
Alias «x» := «expr» Do «body»
The local variable declarations are used to create a local variable, «x», that will be used to hold a handle to an Analytica object, such that wherever the identifier «x» is used within the «body» expression, «x» serves as an alias of the object pointed to by the handle.
The expression «expr» is evaluated, and would most commonly return a Handle when this declaration is used.
These are useful when implementing Meta-Inference algorithms.
Alias..Do is exactly equivalent to LocalAlias..Do.
Unlike Var..Do, you cannot declare the allowed dimensions of «x» as part of the declaration. LocalAlias..Do will always require «x» to be atomic, so that if «expr» results in an array of handles, «body» is evaluated repeatedly, once for each handle in the array.
Examples
Using a local alias, you can shorten an expression that uses the value of a variable that has a very long identifier. When doing this, there are two notable uses:
LocalAlias x := Handle(A) Do «body» LocalAlias x := Handle(A,asIndex:True) Do «body»
In both cases, x serves as an alias of A everywhere in «body». If we were to copy the «body» expression, substituting the identifier A for the identifier x, we would obtain the same result as with the first case. The second case, where asIndex:true is declared, makes x serve as an alias to the index A in the case where A's value contains a self-index. The distinction here is tha that when x is used in a value context within «body», the IndexValue(A) is used, rather than the Mid(A) or Sample(A) value.
These two uses of LocalAlias..Do can be used to shorten an expression that makes repeated use of a variable with a long identifier. For example:
LocalAlias x := Handle(Budgetary_allocations); LocalAlias L := Handle(Allocations_Category,asIndex:True); (x[L='daily']*365 + x[L='weekly']*52 + x[L='monthly']*12) * x[L='frequency of request']
Use with Non-Handles
LocalAlias..Do does not require «expr» to evaluate to a handle. When «expr» evaluates to something other than a Handle, «x» represents the atomic value that resulted, which might be numeric, textual, etc. So, for example, the following is perfectly acceptable:
LocalAlias g:=(1+sqrt(5))/2 do Floor(g^17)
Note that the value «x» will always be atomic. If the result of «expr» is array-valued, Analytica will iterate, successively assigning «x» to each atomic value and evaluting «body» each time, collecting all the results.
Assignment
When «x» appears on the left-hand side of an assignment operator, what happens depends on whether «x» has been set to a Handle or not. When «x» is set to a Handle, as it most commonly would be, then the assignment attempts to alter the variable aliased by «x». If «x» has previous been assigned a numeric, textual, or other non-handle value, then the local variable value is changed. An attempt to assign to a global variable results in an erro unless the global variable is defined using the ComputedBy function, or the assignment is occuring from within a button script or a User-Defined Function called from a button script.
Example:
Variable A Definition: ComputedBy(C) Variable B Definition: 0 Variable C Definition: LocalAlias x := 7; x := Handle(A); x := 5; x := x + 1; x := Handle(C); x := 17
When C is evaluated, the local variable originally contains the value 7. It is then set to be an alias of A. The third lines sets the definition of the global variable A to 5. The second line sets A to be 6. The third line sets the value of A to be a handle pointing to the object C. Notice that at this point, the local variable x is still an alias for A -- it has not become an alias for C. Finally the last line changes the value of A yet again, this time to 17.
Once a local variable declared via LocalAlias..Do is set to a handle, it remains an alias of that object for evermore until the local variable falls out of lexical context. Once it is an alias, assignment changes the object pointed to, not what the local points to.
Enable comment auto-refresher