GetEvaluationContext
GetEvaluationContext(index, position)
Returns information about what slice of the currently evaluating expression will be used by the final result. You would not typically use this function in your models; however, it can provide a peek into Analytica's internal state of evaluation. This has application for internal QA testing within Lumina, or it may help you to debug something in that it might help you to understand how evaluation is occurring.
The evaluation context information is used by certain internal speed-up optimizations, allowing Analytica to deduce when certain calculations can be skipped to save time, since the computed value will end up being ignored.
The evaluation context most commonly comes into being when array-abstraction automatically iterates over a index. Slice or Subscript operations may also augment the evaluation context.
When called with no parameters, GetEvaluationContext() returns a list of handles to indexes. For each of these indexes, only one slice of the currently evaluating expression will be retained in the final result.
When a handle to an «index» is provided as the first parameter, it returns the index value indicating which slice along that index will be retained by the final result. When the index is not in the evaluation context, it returns Null. When you pass position: True
, the position along the indicated index is returned.
Examples
This expression returns the full evaluation context:
MetaIndex index := GetEvaluationContext() Do GetEvaluationContext(index)
Function ShowEvalCxt() :=
MsgBox(MetaIndex index := GetEvaluationContext() Do GetEvaluationContext(index))
Index Division := ['Sales', 'Marketing', 'Engineering', 'Manufacturing', 'Accounting']
Variable Budget := Table(Division)(350K, 80K, 140K, 250K, 40K)
Function RelBudget(x: atom)
Definition: ShowEvalCxt(); x/Sum(budget,Division)
Variable PctBudget := RelBudget(Budget)
When PctBudget
is evaluated, you'll see a MsgBox appear 5 times, once for each division, Each time, Division
is the only index, and the evaluation context reveals which division is being calculated.
Gotchas
Consider an expression of the form:
Local tmp := ( LocalIndex inds := GetEvaluationContext() Do .... );
In this case, tmp
will capture the full result of the right-hand side, so GetEvaluationContext will return an empty list of index, even if the result of the Local tmp := ... Do «body»
expression only uses a single slice. You can get around this in this example using instead
Local tmp[ ] := ( LocalIndex inds := GetEvaluationContext() Do .... );
which works because now it can prove that only the current evaluation context's slice of the right-hand side result will actually effect the result of evaluating tmp
's body expression. A full example of this is:
Local dbg[] := (LocalIndex inds := GetEvaluationContext() Do
JoinText( Local ind[]:=inds Do f"{ind}={GetEvaluationContext(ind,true):I}", inds, ', '));
which sets dbg
to a human-readable description of the current evaluation context, such as "I='a', J=4"
.
History
This function was introduced in Analytica 4.6.
Enable comment auto-refresher