Skip to content

Commit d7e68ed

Browse files
authored
Document record punning in patterns and static requirement for modules (#2006)
* document punning and static expressions/decs/patterns/modules bodies
1 parent c809977 commit d7e68ed

File tree

1 file changed

+51
-5
lines changed

1 file changed

+51
-5
lines changed

doc/modules/language-guide/pages/language-manual.adoc

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* [X] Remove Shared type
2525
* [X] Explain dot keys, dot vals and iterators
2626
* [X] Debug expressions
27-
* [ ] Document punning in type record patterns: https://github.com/dfinity-lab/motoko/pull/964
27+
* [X] Document punning in type record patterns: https://github.com/dfinity-lab/motoko/pull/964
2828
* [X] Update ErrorCode section
2929
* [Floats] Literals type and operations
3030
* [ ] Re-section so headings appear in content outline
@@ -539,6 +539,8 @@ The syntax of a _pattern_ is as follows:
539539

540540
<pat-field> ::= object pattern fields
541541
<id> = <pat> field
542+
<id> punned field
543+
<id> : <typ> typed punned field
542544
```
543545

544546

@@ -1222,6 +1224,7 @@ During an upgrade, a trap occuring in the implicit call to `preupgrade()` or `po
12221224
[[decl-seq]]
12231225
=== Sequence of declarations
12241226

1227+
12251228
A sequence of declarations `<dec>;*` occurring in a block, a program or embedded in the `<dec-field>;*` sequence of an object body has type `T`
12261229
provided:
12271230

@@ -1293,6 +1296,10 @@ The object pattern `{ <pat-field>;* }` matches an object value, a collection of
12931296
The set of identifiers bound by each field of the object pattern must be distinct.
12941297
The names of the pattern fields in the object pattern must be distinct.
12951298

1299+
Object patterns support _punning_ for concision.
1300+
A punned field `<id>` is shorthand for `<id> = <id>`; Similarly, a typed, punned field `<id> : <typ> ` is short-hand for `<id> = <id> : <typ>`. Both
1301+
bind the matched value of the field named `<id>` to the identifier `<id>`.
1302+
12961303
Pattern matching fails if one of the pattern fields fails to match the corresponding field value of the object value.
12971304
Pattern matching succeeds if every pattern field matches the corresponding named field of the object value.
12981305
The binding returned by a successful match is the union of the bindings returned by the field matches.
@@ -1409,7 +1416,7 @@ Fields can be declared with `public` or `private` visibility; if the visibility
14091416

14101417
The qualifier `<sort>` (one of `actor`, `module` or `object`) specifies the *sort* of the object's type. The sort imposes restrictions on the types of the public object fields.
14111418

1412-
Let `T = sort { [var0] id0 : T0, ... , [varn] idn : T0 }` denote the type of the object.
1419+
Let `T = <sort> { [var0] id0 : T0, ... , [varn] idn : T0 }` denote the type of the object.
14131420
Let `<dec>;*` be the sequence of declarations embedded in `<dec-field>;*`.
14141421
The object declaration has type `T` provided that:
14151422

@@ -1419,21 +1426,58 @@ The object declaration has type `T` provided that:
14191426
with types `T(id)` for `id` in `Id == Id_private union Id_public`, and
14201427
* `{ id0, ..., idn } == Id_public`, and
14211428
* for all `i in 0 \<= i \<= n`, `[vari] Ti == T(idi)`.
1429+
3. If `<sort>` is `module`, then the declarations in `<dec>;*` must be _static_ (see <<decl-static>>).
14221430

14231431
Note that requirement 1. imposes further constraints on the field types of `T`.
14241432
In particular:
14251433

14261434
* if the sort is `actor` then all public fields must be non-`var` (immutable) `shared` functions (the public interface of an actor can only provide asynchronous messaging via shared functions).
14271435

1428-
Evaluation of `(object|actor)? <id>? =? { <exp-field>;* }` proceeds by
1436+
Evaluation of `<sort>? <id>? =? { <dec-field>;* }` proceeds by
14291437
evaluating the declarations in `<dec>;*`. If the evaluation of `<dec>;*` traps, so does the object declaration.
14301438
Otherwise, `<dec>;*` produces a set of bindings for identifiers in `Id`.
14311439
let `v0`, ..., `vn` be the values or locations bound to identifiers `<id0>`, ..., `<idn>`.
14321440
The result of the object declaration is the object `v == sort { <id0> = v1, ..., <idn> = vn}`.
14331441

14341442
If `<id>?` is present, the declaration binds `<id>` to `v`. Otherwise, it produces the empty set of bindings.
14351443

1436-
// TBR do we actually propagate trapping of actor creation?
1444+
[[decl-static]]
1445+
==== Static declarations
1446+
1447+
A declaration is _static_ if it is:
1448+
1449+
* a `type` declaration, or
1450+
* a `class` declaration, or
1451+
* a `let` declaration with a static pattern and a static expression, or
1452+
* a module, function or object declaration that desugars to a static `let` declaration, or
1453+
* a static expression, or
1454+
* an `ignore` with static expression.
1455+
1456+
An expression is _static_ if it is:
1457+
1458+
* a literal expression, or
1459+
* a tuple of static expressions, or
1460+
* an object of static expressions, or
1461+
* a variant or option with a static expression, or
1462+
* an immutable array, or
1463+
* field access and projection from a static expression, or
1464+
* a module expression, or
1465+
* a function expression, or
1466+
* a static declaration, or
1467+
* a block, all of whose declarations are static, or
1468+
* a type annotation with a static expression.
1469+
1470+
A pattern is _static_ if it is:
1471+
1472+
* an identifier, or
1473+
* a wildcard, or
1474+
* a tuple of static patterns, or
1475+
* type annotation with a static pattern.
1476+
////
1477+
why not record patterns?
1478+
////
1479+
1480+
Static phrases are designed to be side-effect free, allowing the coalescing of duplicate library imports (a.k.a deduplication).
14371481

14381482
[[decl-func]]
14391483
=== Function declaration
@@ -2044,7 +2088,9 @@ Note: type annotations have no-runtime cost and cannot be used to perform the (c
20442088

20452089
The declaration expression `<dec>` has type `T` provided the declaration `<dec>` has type `T`.
20462090

2047-
Evaluating the expression `<dec>` proceed by evaluating `<dec>`, returning the result of `<dec>` but discarding the bindings introduced by `<dec>` (if any).
2091+
Evaluating the expression `<dec>` proceeds by evaluating `<dec>`, returning the result of `<dec>` but discarding the bindings introduced by `<dec>` (if any).
2092+
2093+
(The expression `<dec>` is actually shorthand for the block expression `{ <dec> }`.)
20482094

20492095
[[exp-debug]]
20502096
=== Debug

0 commit comments

Comments
 (0)