Keeping text literals consistent with index labels

Revision as of 21:36, 13 November 2025 by Lchrisman (talk | contribs) (Created page with "Analytica includes many advanced features for automatically updating expressions as you rename identifiers, change index values used by tables (see Table Splicing) or othe...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Analytica includes many advanced features for automatically updating expressions as you rename identifiers, change index values used by tables (see Table Splicing) or other expressions, or change inputs used by previously computed calculations. These features help you to focus on your model logic and avoid mistakes.

One change that can impact logic in other expressions throughout your model is a name change to an index label. This occurs when you have defined an index to be a List of Text, and then you have used the text values in the definitions (or other expressions) in other variables. For example:

Index Power_type ::= [ 'coal', 'gas', 'hydro', 'nuclear', 'solar', 'wind' ]
Variable Nems2_offset ::= If Power_type = 'solar' Then ...

This partial example illustrates a case where a label from the Power_type index, 'solar', appears in the definition of a different variable. At some point you might change one or more label names in Power_type which would cause the definition in Nems2_offset to become incorrect (and not necessarily in a way that throws an error).

In the above example, Analytica will automatically figure this out and rewrite the definition of Nems2_offset when you change the 'solar' label in Power_type. However, in other cases it can be far less obvious that text literal is associated with an index, such that this will not happen automatically. This would include the following.

Variable Incentive_discount ::=
     Local desired_type := If abs(latitude)<35 Then "solar" Else "nuclear";
     If Power_type == desired_type Then Solar_discount Else Nuclear_discount

To ensure that Analytica catches the association of these text literals with the index labels, you can use the index-label syntax instead:

Variable Incentive_discount ::=
     Local desired_type := If abs(latitude)<35 Then Power_type . "solar" Else Power_type . "nuclear";
     If Power_type == desired_type Then Solar_discount Else Nuclear_discount

The index-label notation provides two benefits:

  1. When the label in the index (Power_type) changes, it knows to rewrite the expression with the new label.
  2. When Power_type . "solar" is evaluated, it double checks that the index contains the label and issues a warning if not, helping you to find mistakes. The result of the evaluation is the text value "solar".
Comments


You are not allowed to post comments.