Skip to main content

Node FieldSelection

rascal-0.42.1

Synopsis

Select a field from a node by its field name.

Syntax

Exp . Name

Types

ExpNameExp . Name
nodeLᵢTᵢ

Description

Field selection applies to nodes with keyword fields. Exp should evaluate to a node with keyword field Name. Then field selection returns the value of that field. Name stands for itself and is not evaluated. The static type of Exp.Name is always value.

Examples

rascal>n = "myNode"(val="42", age=84);
node: "myNode"(
val="42",
age=84)
rascal>n.val
value: "42"
rascal>n.age
value: 84

When a field is not present, an exception is thrown:

rascal>n = "myNode"(val="42", age=84);
node: "myNode"(
val="42",
age=84)
rascal>n.height
|prompt:///|(0,1,<1,0>,<1,1>): NoSuchField("height")
at $(|prompt:///|(0,31,<1,0>,<1,31>))

Benefits

  • untyped node values can be used to easily import external structured (e.g with fields) data sources without first having to validate them.
  • Field selection also works for Constructors, Relations and Tuples, and the builtin fields of Location and Date Time. All with similar syntax and semantics.

Pitfalls

  • the static return type of any keyword field on node is always value. If you need more accurate types, consider defining an Algebraic Data Type and add keyword parameters to Constructors.