Dot syntax

From MiniScript Wiki
Jump to navigation Jump to search

Dot syntax refers to a way of indexing into a map by putting the key after the map reference, joined by the dot operator. For example, instead of myMap["entry"], with dot syntax you would write myMap.entry.

The key must be a valid MiniScript identifier, that is, it must begin with a letter, and contain only letters, numbers, and underscores.

When the map entry is a function reference, the function is automatically invoked (unless this invocation is suppressed with the @ operator). Moreover, to support object-oriented programming, a method invoked in this way is called with self bound to the map on the left-hand side of the dot. In the example of myMap.entry, the function referenced by myMap["entry"] is invoked with self bound to the same map as myMap.

A chain of dot operations is evaluated one step at a time left to right.

For example, given the code:

foo.bar.baz

MiniScript proceeds in the following steps:

  1. foo.bar is evaluated by looking up "bar" in </c>foo</c> (and in the event that this references a function, it's invoked with self bound to foo.
  2. .baz is evaluated by looking up "baz" in the result of step 1 (which should be a map). (And if "baz" references a function, it's invoked with self bound to the map from step 1.)

This can continue for an arbitrarily long chain of dot operators.