Attrib of Obj
Attrib Of Obj
Returns the value of attribute «Attrib» of object «Obj». «Attrib» and «Obj» may each be an identifier, a text value containing identifiers, or a handle to the attribute or object. For example:
Units OF Time → 'Years'
'Units' OF 'Time' → 'Years'
'Units' OF HandleFromIdentifier('Time') → 'Years'
If the requested attribute is not set for that object, it returns Null. This works if «Attrib» or «Obj» are arrays of attribute or object names or handles.
Computing «obj»
Sometimes you may want to use an expression for «obj» rather than just a literal identifier , especially when doing Meta-Inference. For example:
Decision A := 'Yes'
Variable B := Handle(A)
Class OF B → Variable
You might have expected that the second expression would return Decision
, the class of variable 'A'
whose handle is the value of B
. But the parser naturally assumes you want the attribute of the object B
, not its value. If you want to get the value of the variable or expression in «Obj», you need use an expression that is not just the name of a variable. You can simply put parentheses around the identifier:
Class OF (B) → Decision
This can be particularly useful if you want to get an attribute of a list of objects:
INDEX Vars := ListOfHandles(X, Y, Z)
Class OF (Vars) → Array(Vars, [Decision, Variable, Index])
It's fine to use the name of a variable (or more complex expression) for «attrib». If «attrib» is not simply the identifier of an attribute, it evaluates the expression. It expects to find a handle to an attribute, or a text value with the identifier of an attribute, or an array of such handles or text values, for example:
Variable Attribs := ['Class', 'Identifier', 'Value']
Attribs OF A→ Array(Attribs, ['Decision', 'A', 'Yes'])
You can use OF to get attributes of a function:
Description OF Evaluate
returns the description of the evaluate function, while
Description OF Evaluate('Va1')
returns the description of Va1
.
The OF operator parses in a right-associative fashion, so that:
Class OF Value OF B→ Decision
parses as:
Class OF (Value OF B)→ Decision
OF binds more tightly than arithmetic operators, but less tightly than the Subscript/Slice Operator. So, for example, to access an attribute of a local index of an object, parens are not necessary:
Index I := 1..2;
Description OF I := "A simple index";
Var A := I^2;
Description OF A.I & "=" & A
The last line parses as
((Description OF (A.I)) & "=") & A
Assigning Values to Attributes
You can set the value of attributes using the syntax:
attrib OF obj := expr
As with the standard assignment), you can use this only in the OnClick attribute of a Button, OnChange of a Variable, Script attribute (obsolete), or a Function called from one of these. You may also assign to an attribute of a Local variable within a Definition.
You cannot assign to read-only attributes, usually internally computed attributes, like Inputs, and Outputs. You can assign a handle to a module IsIn, which has the effect of moving the object into that module. You can assign to Value of a variable, but that is dangerous because it may be inconsistent with its Definition.
As with getting an attribute of an object, «attrib» and «obj» may each be a simple identifier, text containing an identifier, or a handle to the attribute or object. And they may be statically bound or dynamically resolved in the same way. However, for attribute assignment, «attrib» and «obj» must be atoms. It does NOT automatically array abstract.
If «expr» is an array, the array value is assigned to the attribute. For most attributes, «expr» should to evaluate to text, although some attributes expect numbers. An example is this:
NodeColor OF Va1 := '16000,8000,65535'
Because Analytica imposes restrictions on side-effects, so that it can maintain dependencies among variables consistently, assignment to attributes of global objects is not permitted while a variable is being evaluated. You may assign to attributes of local indexes (declared using Index..Do).
To remove an attribute value, assign Null to it:
Units of Va1 := Null
Note: Internally, Analytica distinguishes between an attribute set to the special value Null, and an attribute that has no value. However, it is not possible to set an attribute value to Null from an Analytica expression (although you can set value to lists or arrays containing Null elements).
You may only assign a text value to the definition attribute, e.g.:
Definition OF Va1 := "A + B"
Note that when you assign directly to a variable:
Va1 := "A + B"
it sets the definition to the text value 'A+B'
(with quotes), rather than the expression A+B
(without quotes) that depends on the values of A and B.
No dependency maintenance for attributes other than Definition and Identifier
Analytica automatically maintains dependencies between Definitions to values of dependent variables, so that if you change the definition of variable, A
, it will invalidate the value of any variable B
that depends on the value of A
, and recompute B
when needed. It also automatically propagates any change to an identifier to update any definitions that use that identifier. This does not work for other attributes: So if you (or some Analytica code) changes, say, the Title of A
, it will not invalidate or recompute B
if it depends on the Title of A
:
A := 20
Units OF A := 'KW'
B := IF Units OF A = 'KW' THEN 1000*A ELSE IF Units of A = 'MW' THEN 1M*A ELSE A
B → 20000
{ Because Units of A are KW }Units OF A := 'MW'
B → 20000
{ Changing Units of A did not cause B to be recomputed }
Enable comment auto-refresher