Difference between revisions of "Dot syntax"

From MiniScript Wiki
Jump to navigation Jump to search
(Created page with "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 <c>myMap["entry"]</c>, w...")
 
 
Line 3: Line 3:
 
The key must be a valid MiniScript identifier, that is, it must begin with a letter, and contain only letters, numbers, and underscores.
 
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 <c>[[self]]</c> bound to the map on the left-hand side of the dot.  In the example of <c>myMap.entry</c>, the function referenced by <c>myMap["entry"]</c> is invoked with <c>[[self]]</c> bound to the same map as <c>myMap</c>.
 +
 +
A chain of [[Dot syntax|dot operations]] is evaluated one step at a time left to right.
 +
 +
For example, given the code:
 +
<ms>foo.bar.baz</ms>
 +
 +
MiniScript proceeds in the following steps:
 +
# <c>foo.bar</c> is evaluated by looking up "bar" in </c>foo</c> (and in the event that this references a function, it's invoked with <c>[[self]]</c> bound to <c>foo</c>.
 +
# <c>.baz</c> 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 <c>[[self]]</c> bound to the map from step 1.)
 +
 +
This can continue for an arbitrarily long chain of dot operators.
  
 
[[Category:Language]]
 
[[Category:Language]]

Latest revision as of 16:42, 5 October 2023

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.