0.16.0
-
motoko (
moc)-
Breaking change: add new type constructor
weak Tfor constructing weak references.Prim.allocWeakRef: <T>(value : T) -> weak T Prim.weakGet: <T>weak T -> ?(value : T) Prim.isLive: weak Any -> Bool
A weak reference can only be allocated from a value whose type representation is always a heap reference;
allowWeakRefwill trap on values of other types.
A weak reference does not count as a reference to its value and allows the collector to collect the value once no other references to it remain.
weakGetwill returnnull, andisLivewill return false once the value of the reference has been collected by the garbage collector.
The type constructorweak Tis covariant.Weak reference operations are only supported with --enhanced-orthogonal-persistence and cannot be used with the classic compiler.
-
bugfix: the EOP dynamic stable compatibility check incorrectly rejected upgrades from
Nullto?T(#5404). -
More explanatory upgrade error messages with detailing of cause (#5391).
-
Improved type inference for calling generic functions (#5180).
This means that type arguments can be omitted when calling generic functions in most common cases.
For example:let ar = [1, 2, 3]; Array.map(ar, func x = x * 2); // Needs no explicit type arguments anymore!
Previously, type arguments were usually required when there was an anonymous not annotated function in arguments.
The reason being that the type inference algorithm cannot infer the type offuncs in general,
e.g.func x = x * 2cannot be inferred without knowing the type ofx.Now, the improved type inference can handle such
funcs when there is enough type information from other arguments.
It works by splitting the type inference into two phases:- In the first phase, it infers part of the instantiation from the non-
funcarguments.
The goal is to infer all parameters of thefuncarguments, e.g.xin the example above.
Thearargument in the example above is used to infer the partial instantiationArray.map<Nat, O>, leaving the second type argumentOto be inferred in the second phase.
With this partial instantiation, it knows thatx : Nat. - In the second phase, it completes the instantiation by inferring the bodies of the
funcarguments; assuming that all parameters were inferred in the first phase.
In the example above, it knows thatx : Nat, so inferring the bodyx * 2will infer the typeOto beNat.
With this, the full instantiationArray.map<Nat, Nat>is inferred, and the type arguments can be omitted.
Limitations:
-
Invariant type parameters must be explicitly provided in most cases.
e.g.VarArray.mapmust have the return type annotation:let result = VarArray.map<Nat, Nat>(varAr, func x = x * 2);
Or the type of the result must be explicitly provided:
let result : [var Nat] = VarArray.map(varAr, func x = x * 2);
-
When there is not enough type information from the non-
funcarguments, the type inference will not be able to infer thefuncarguments.
However this is not a problem in most cases.
- In the first phase, it infers part of the instantiation from the non-
-