LocalAlias..Do


LocalAlias x := expr Do body

Alias x := expr Do body

You use LocalAlias to declare a local variable «x» whose definition «expr» can be a handle to an Analytica object. Wherever you use the identifier «x» in the «body» expression, «x» is an alias of the object pointed to by the handle. So you can use «x» in an expression wherever you would expect a variable rather than a value. For example,

LocalAlias x := Handle(I) Do Sum(Y, I)

works if I is the identifier of an Index.

If you use LocalAlias in the Onclick attribute of a button, or a function called from a button, you can use this to assign to a global variable:

LocalAlias x := Handle(A) DO x := 100

The effect is to assign the value 100 to the global variable A.

LocalAlias x := expr DO body requires «x» to be atomic -- i.e. to contain a single handle. If «expr» results in an array of handles, it will evaluate «body» repeatedly, with «x» set successively to each element of the array. So, unlike Var..DO, you cannot declare the allowed dimensions of «x» as part of the declaration.

These are useful when implementing Meta-Inference algorithms.

Alias..Do is exactly equivalent to LocalAlias..Do.

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 evaluating «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 error unless the global variable is defined using the ComputedBy function, or the assignment is occurring 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.

Other notes

In Analytica 4.2, a parameter of a User-Defined Function declared as (x : Variable) or (x : Object) is treated the same as a local declared using LocalAlias..Do (assigned a handle with asIndex:false). So, for example, assignment to that parameter, x :=..., would be an assignment to the global variable object passed in to the function, rather than a change to the parameter value.

See Also

Comments


You are not allowed to post comments.