Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Dot

Records and maps with string keys are dereferenced with the dot operator . as is customary in other languages. The syntax is

<expr> . <id>

where <expr> is an expresson resulting in a dereferenceable value and <id> is an identifier representing the field name of a record or a string key of a map.

Dereferenceable values include records, maps with keys of type string, and type values that are of type record.

The result of a dot expression is

  • the value of the indicated field for a record type,
  • the value of the indicated entry for a map with string keys, or
  • the type value of the indicated field for a record type value.

When a field or key is referenced in a dereferenceable type but that field or key is not present, then error("missing") is the result.

If a non-dereferenceable type is operated upon with the dot operator, then a compile-time error results for statically typed data and a structured error results for dynamic data.

Note that identifiers containing unusual characters can be represented by enclosing the identifier in backtick quotes, e.g.,

x.`a b`

references the field or key whose name is a b.

Alternatively, index syntax may be used to access the field as a string value, e.g.,

x["a b"]

If a field name is not representable as an identifier, then indexing may be used with a quoted string to represent any valid field name. Such field names can be accessed using this with an index-style reference, e.g., this["field with spaces"].

Examples


Derefence a map, a record, and a record type

# spq
values this.x.y
# input
|{"x":{y:1},"y":2}|
{x:{y:1},y:2}
<{x:{y:int64},y:int64}>
# expected output
1
1
<int64>

Use backtick quotes for identifiers with special characters

# spq
values x.`a b`, x["a b"]
# input
{x:{"a b":123}}
# expected output
123
123