You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/modules/language-guide/pages/language-manual.adoc
+51-5Lines changed: 51 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@
24
24
* [X] Remove Shared type
25
25
* [X] Explain dot keys, dot vals and iterators
26
26
* [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
28
28
* [X] Update ErrorCode section
29
29
* [Floats] Literals type and operations
30
30
* [ ] Re-section so headings appear in content outline
@@ -539,6 +539,8 @@ The syntax of a _pattern_ is as follows:
539
539
540
540
<pat-field> ::= object pattern fields
541
541
<id> = <pat> field
542
+
<id> punned field
543
+
<id> : <typ> typed punned field
542
544
```
543
545
544
546
@@ -1222,6 +1224,7 @@ During an upgrade, a trap occuring in the implicit call to `preupgrade()` or `po
1222
1224
[[decl-seq]]
1223
1225
=== Sequence of declarations
1224
1226
1227
+
1225
1228
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`
1226
1229
provided:
1227
1230
@@ -1293,6 +1296,10 @@ The object pattern `{ <pat-field>;* }` matches an object value, a collection of
1293
1296
The set of identifiers bound by each field of the object pattern must be distinct.
1294
1297
The names of the pattern fields in the object pattern must be distinct.
1295
1298
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
+
1296
1303
Pattern matching fails if one of the pattern fields fails to match the corresponding field value of the object value.
1297
1304
Pattern matching succeeds if every pattern field matches the corresponding named field of the object value.
1298
1305
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
1409
1416
1410
1417
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.
1411
1418
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.
1413
1420
Let `<dec>;*` be the sequence of declarations embedded in `<dec-field>;*`.
1414
1421
The object declaration has type `T` provided that:
1415
1422
@@ -1419,21 +1426,58 @@ The object declaration has type `T` provided that:
1419
1426
with types `T(id)` for `id` in `Id == Id_private union Id_public`, and
1420
1427
* `{ id0, ..., idn } == Id_public`, and
1421
1428
* 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>>).
1422
1430
1423
1431
Note that requirement 1. imposes further constraints on the field types of `T`.
1424
1432
In particular:
1425
1433
1426
1434
* 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).
1427
1435
1428
-
Evaluation of `(object|actor)? <id>? =? { <exp-field>;* }` proceeds by
1436
+
Evaluation of `<sort>? <id>? =? { <dec-field>;* }` proceeds by
1429
1437
evaluating the declarations in `<dec>;*`. If the evaluation of `<dec>;*` traps, so does the object declaration.
1430
1438
Otherwise, `<dec>;*` produces a set of bindings for identifiers in `Id`.
1431
1439
let `v0`, ..., `vn` be the values or locations bound to identifiers `<id0>`, ..., `<idn>`.
1432
1440
The result of the object declaration is the object `v == sort { <id0> = v1, ..., <idn> = vn}`.
1433
1441
1434
1442
If `<id>?` is present, the declaration binds `<id>` to `v`. Otherwise, it produces the empty set of bindings.
1435
1443
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).
1437
1481
1438
1482
[[decl-func]]
1439
1483
=== Function declaration
@@ -2044,7 +2088,9 @@ Note: type annotations have no-runtime cost and cannot be used to perform the (c
2044
2088
2045
2089
The declaration expression `<dec>` has type `T` provided the declaration `<dec>` has type `T`.
2046
2090
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> }`.)
0 commit comments