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
+89-25Lines changed: 89 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -382,18 +382,29 @@ An import introduces a resource named '<id>?' referring to a local source module
382
382
The syntax of a _library_ (that can be referenced in an import) is as follows:
383
383
384
384
```bnf
385
-
<lib> ::= library
386
-
<imp>;* module <id>? =? { <dec-field>;* }
385
+
<lib> ::= library
386
+
<imp>;* module <id>? <obj-body> module
387
+
<imp>;* <shared-pat>? actor class <id> <typ-params>? <pat> (: <typ>)? <class-body> actor class
387
388
```
388
389
389
-
A library `<lib>` is a sequence of imports `<imp>;*` followed by a named or anonymous (module) declaration `module <id>? =? { <dec-field>;* }`.
390
+
A library `<lib>` is a sequence of imports `<imp>;*` followed by:
390
391
391
-
Libraries stored in {ext} files may be referenced by `import`s.
392
+
* a named or anonymous (module) declaration; or
393
+
* a named actor class declaration.
392
394
393
-
The name `<id>?` is only significant within the library and does not determine the name of the library when imported.
395
+
Libraries stored in {ext} files may be referenced by `import` declarations.
396
+
397
+
In a module library, the optional name `<id>?` is only significant within the library and does not determine the name of the library when imported.
394
398
Instead, the imported name of a library is determined by the `import` declaration, giving clients of the library the freedom to
395
399
choose library names (e.g. to avoid clashes).
396
400
401
+
An actor class library, because it defines both a type constructor and a function with name `<id>`, is imported as a module defining both a type and a function named `<id>`.
402
+
The name `<id>` is mandatory and cannot be omitted.
403
+
The imported actor class constructor is asynchronous, with return type `async T`. Here `T` is the inferred or supplied type of the class body.
404
+
Because actor construction is asynchronous, an instance of an imported actor class can only be created in an asynchronous context
405
+
(i.e. in the body of a (non-`query`) `shared` function or `async` expression).
406
+
407
+
397
408
[[syntax-decls]]
398
409
=== Declaration syntax
399
410
@@ -405,10 +416,17 @@ The syntax of a _declaration_ is as follows:
405
416
ignore <exp> ignore
406
417
let <pat> = <exp> immutable
407
418
var <id> (: <typ>)? = <exp> mutable
408
-
<sort> <id>? =? { <dec-field>;* } object
419
+
<sort> <id>? =? <obj-body> object
409
420
<shared-pat>? func <id>? <typ-params>? <pat> (: <typ>)? =? <exp> function
410
421
type <id> <typ-params>? = <typ> type
411
-
<shared-pat>? <sort>? class <id> <typ-params>? <pat> (: <typ>)? =? { <exp-field>;* } class
422
+
<shared-pat>? <sort>? class <id>? <typ-params>? <pat> (: <typ>)? <class-body> class
(That is, function type `T` is a subtype of function type `U` if they have same `<shared>?` qualification, they have the same type parameters (modulo renaming) and assuming the bounds in `U`,
993
1011
every bound in `T` supertypes the corresponding parameter bound in `U` (contra-variance), the domain of `T` supertypes the domain of `U` (contra-variance) and the range of `T` subtypes
994
1012
the range of `U` (co-variance).)
995
1013
+
996
-
* `T` (respectively `U`) is a constructed type `C<V0,...VN>` that is equal, by definition of type constructor `C`, to `W`, and `W <: U` (respectively `U <: W`).
1014
+
* `T` (respectively `U`) is a constructed type `C<V0,..., Vn>` that is equal, by definition of type constructor `C`, to `W`, and `W <: U` (respectively `U <: W`).
997
1015
998
1016
* For some type `V`, `T <: V` and `V <: U` (_transitivity_).
999
1017
@@ -1061,18 +1079,59 @@ A program evaluates by (transitively) evaluating the imports, binding their valu
1061
1079
[[libraries]]
1062
1080
=== Libraries
1063
1081
1064
-
A library `<imp>;* module <id>? =? { <dec-field>;* }` is a sequence of imports `<import>;*` followed by a single module declaration.
1082
+
Restrictions on the syntactic form of modules means that libraries can have no side-effects.
1083
+
1084
+
The imports of a library are local and not re-exported in its interface.
1085
+
1086
+
Multiple imports of the same library can be safely deduplicated without loss of side-effects.
1087
+
1088
+
==== Module libraries
1089
+
1090
+
A library `<imp>;* module <id>? <obj-body>` is a sequence of imports `<import>;*` followed by a single module declaration.
1065
1091
1066
1092
A library has module type `T` provided
1067
1093
1068
-
* `module <id>? =? { <dec-field>;* }` has (module) type `T` under the static environment induced by the imports in `<import>;*`.
1094
+
* `module <id>? <obj-body>` has (module) type `T` under the static environment induced by the imports in `<import>;*`.
1069
1095
1070
-
The imports of a library are local and not re-exported in its interface.
1096
+
A module library evaluates by (transitively) evaluating its imports, binding their values to the identifiers in `<imp>;*` and then evaluating `module <id>? <obj-body>`.
1071
1097
1072
-
A library evaluates by (transitively) evaluating its imports, binding their values to the identifiers in `<imp>;*` and then evaluating the sequence of declarations in `<dec>;*`.
1098
+
==== Actor class libraries
1073
1099
1074
-
Restrictions on the syntactic form of modules means that libraries can have no side-effects.
1075
-
Multiple imports of the same library can be safely deduplicated without loss of side-effects.
1100
+
////
1101
+
This ugly asyncification of actor class imports below will go away once we change actor class definitions to be asynchronous. This is a temporary kludge.
1102
+
////
1103
+
1104
+
The actor class library `<imp>;* <dec>` where `<dec>` is of the form `<shared-pat>? actor class <id> <typ-params>? <pat> (: <typ>)? <class-body>` has type:
1105
+
1106
+
```
1107
+
module {
1108
+
type <id> = T;
1109
+
<id> : (U1,...,Un) -> async T // asynchronous!
1110
+
}
1111
+
```
1112
+
1113
+
provided that:
1114
+
1115
+
* the actor class declaration `<dec>` has function type `(U1, ..., Un) -> T` under the static environment induced by the imports in `<import>;*`.
1116
+
1117
+
Notice that the imported type of the function `<id>` is the *asynchronous* version of its declared type.
1118
+
1119
+
An actor class library evaluates by (transitively) evaluating its imports, binding their values to the identifiers in `<imp>;*`, and evaluating the (derived) module:
1120
+
1121
+
```
1122
+
module {
1123
+
public type <id> = T;
1124
+
public func <id>(a1 : U1, ..., an : Un) : async T { // asynchronous!
1125
+
(<dec>)(a1, ..., an)
1126
+
}
1127
+
}
1128
+
```
1129
+
1130
+
The imported module contains an asynchronous implementation of function `<id>`.
1131
+
1132
+
On the Internet Computer, calling `await <id>(<exp1>, ..., <expn>)`, installs a fresh instance of the actor class as an isolated IC canister, passing the values of `<exp1>`, ..., `<expn>`
1133
+
as installation arguments, and returns a reference to a (remote) actor of type `T`.
1134
+
Installation is (necessarily) asynchronous.
1076
1135
1077
1136
[[imports]]
1078
1137
=== Imports and Urls
@@ -1163,7 +1222,7 @@ During an upgrade, a trap occuring in the implicit call to `preupgrade()` or `po
1163
1222
[[decl-seq]]
1164
1223
=== Sequence of declarations
1165
1224
1166
-
A sequence of declarations `<dec>;*` occurring in a block, a program or the `exp-field;*` sequence of an object declaration has type `T`
1225
+
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`
1167
1226
provided:
1168
1227
1169
1228
* `<dec>;*` is empty and `T == ()`; or
@@ -1345,13 +1404,13 @@ In scope of the declaration `type C < X0<:T0>, ..., Xn <: Tn > = U`, any well-
1345
1404
[[decl-obj]]
1346
1405
=== Object declaration
1347
1406
1348
-
Declaration `<sort> <id>? =? { <exp-field>;* }` declares an object with optional identifier `<id>` and zero or more fields `<exp-field>;*`.
1407
+
Declaration `<sort> <id>? <obj-body>`, where `<obj_body>` is of the form `=? { <dec-field>;* }`, declares an object with optional identifier `<id>` and zero or more fields `<dec-field>;*`.
1349
1408
Fields can be declared with `public` or `private` visibility; if the visibility is omitted, it defaults to `private`.
1350
1409
1351
1410
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.
1352
1411
1353
1412
Let `T = sort { [var0] id0 : T0, ... , [varn] idn : T0 }` denote the type of the object.
1354
-
Let `<dec>;*` be the sequence of declarations in `<exp_field>;*`.
1413
+
Let `<dec>;*` be the sequence of declarations embedded in `<dec-field>;*`.
1355
1414
The object declaration has type `T` provided that:
1356
1415
1357
1416
1. type `T` is well-formed for sort `sort`, and
@@ -1395,20 +1454,25 @@ Named function definitions are recursive.
1395
1454
[[decl-class]]
1396
1455
=== Class declaration
1397
1456
1398
-
The declaration `<shared-pat>? <sort>? class <id> <typ-params>? <pat> (: <typ>)? =? <id_this>? { <exp-field>;* }` is sugar for pair of a type and function declaration:
1457
+
////
1458
+
TODO: revise once actor class declaration async
1459
+
////
1460
+
1461
+
The _class_ declaration `<shared-pat>? <sort>? class <id>? <typ-params>? <pat> (: <typ>)? <class-body>` is sugar for pair of a type and function declaration:
* `<shared-pat>?`, when present, requires `<sort>` == `actor`, and provides access to the `caller` of an `actor` constructor, and
1409
1472
* `<typ-args>?` is the sequence of type identifiers bound by `<typ-params>?` (if any), and
1410
1473
* `<typ-field>;*` is the set of public field types inferred from `<dec-field>;*`.
1411
-
* `<id_this>?` is the optional `this` parameter of the object instance.
1474
+
* `<obj-body>` is the object body of `<class-body>`.
1475
+
* `<id_this>?` is the optional _this_ (a.k.a _self_), parameter of `<class-body>`.
1412
1476
1413
1477
Note `<shared-pat>?` must not be of the form `shared query <pat>?`: a constructor, unlike a function, cannot be a query.
1414
1478
@@ -1481,7 +1545,7 @@ For equality and inequality, the meaning of `v1 <relop> v2` depends on the compi
1481
1545
=== Tuples
1482
1546
1483
1547
Tuple expression `(<exp1>, ..., <expn>)` has tuple type `(T1, ..., Tn)`, provided
1484
-
`<exp1>`, ..., `<expN>` have types `T1`, ..., `Tn`.
1548
+
`<exp1>`, ..., `<expn>` have types `T1`, ..., `Tn`.
1485
1549
1486
1550
The tuple expression `(<exp1>, ..., <expn>)` evaluates the expressions `exp1` ... `expn` in order, trapping as soon as some expression `<expi>` traps. If no evaluation traps and `exp1`, ..., `<expn>` evaluate to values `v1`,...,`vn` then the tuple expression returns the tuple value `(v1, ... , vn)`.
0 commit comments