Arrow operator

x->member

The arrow operator accesses a member or invokes a member function of an atomic value. This can be distinguished from the dot operator, A.I which operates on A at the array level. For example, suppose x is an array, then

x->member

would array abstract, applying the operator separately to every cell value of x. Whenever x is Null, the result value is Null.

The arrow can be typed either as a sequence of two characters, ->, or as the single right-arrow character, . To type the right-arrow character in Analytica, type the sequence:

\rarr[TAB]

where [TAB] is the tab key.

This operator only makes sense on certain atomic data types. Data types that include member fields (so that the variant x→member is meaningful) include:

Member assignment

In some contexts, with some data types, you can assign to a member using the syntax

x->member := newValue;

The right-hand side here is an expression that is evaluated.

x → memberFn(...)

Some data types include named member functions that can be called using the syntax

x -> memberFn( a, b, c )

Unlike standard function calls, the Analytica parser cannot detect whether the member function exists or has a compatible set of parameters. These determinations occur at evaluation time.

Some data types that have named member functions include:

x → (...)

Some data types are callables themselves (rather than having a named member function that is callable). These are called using the syntax

x -> (a,b,c)

Data types that are directly callable include:

x → [i]

New to Analytica 6.6

Some data types have indexable content in which internal elements can be accessed using the syntax, e.g.,

x->[5]

Data types in this category include:

  • Text values, e.g., "Hello world"->[5] returns "o".
    This is the same as SelectText("Hello world", 5, 5)
  • Binary data terms, in which x->[5] returns the fifth byte as an integer between 0 and 255.
  • Structs with a custom arrow operator.
  • Python sequences including python tuple, list, str, range, bytes, bytearray, etc.

In general, the atomic subscript uses a 1-based convention (where x->[1] is the first element), even (for example) when applied to a python object (whereas in a python expression it would be zero-based).

x → [m..n]

The dot-dot operator can be placed inside the brackets to indicate that you want a subsequence rather than a single element. In addition, either «m» or «n» can be omitted to indicate "from the beginning" or "to the end". For example:

  • "Hello world"->[5..8] returns "o wo"
  • "Hello world"->[.. 8] returns "Hello wo"
  • "Hello world"->[5..] returns "o world"

When x is a Binary data term and a dot-dot is used in the brackets, the result is a new, smaller, binary data term.

The use of dot-dot inside the brackets is a special case as is not the same as these:

These examples would return lists of elements rather than sub-sequences of the original.

`->`( x, member) syntax

The arrow operator can be called using a function call syntax. In this syntax, the function name is `->` in which the name uses backquotes. With this syntax, you cannot use the → character (\rarr[TAB]) or `→` instead of `->` like you can with x→member.

A key difference between this syntax and x->member is that here «member» is evaluated, where as in x->member the name is explicitly listed. Thus, for example, these are equivalent:

  • x->val
  • x→val
  • `->`(x, "val")
  • Local member := "val" Do `->`(x, member)

Thus, this syntax is useful when you want to compute the member name.

defVal

With the function call syntax, you can also specify a default value to be returned when the object doesn't have the indicated member.

`->`( x, "memberName", defVal: Null )

When «defVal» is not specified (or the x->member syntax is used) an error occurs when x doesn't have the requested member.

See also

Comments


You are not allowed to post comments.