dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
515 stars 97 forks source link

experiment: add optional stability modifiers before body to control default #4766

Closed crusso closed 15 hours ago

crusso commented 1 week ago

Investigate with prefixing body of actor or actor class with optional, default stability modifier, supporting the opt-in inversion from flexible to stable, without changing the semantics of existing code.

(Prefixing the actor keyword introduces hundreds of shift/reduce conflicts, so I tried this instead. Perhaps the conflicts can be avoid by some grammar refactoring...)

Note: we only allow the stable modifier on simple let patterns bind a single identier and (all) mutable variables, so care must be taken not to default to stable in other cases...

actor Counter stable {

  var count : Nat = 0; // defaults to stable

  public func inc() : async Nat {
    count += 1;
    count
  };

  // let f = func(){}; // rejected as unstable

}

and

actor class Counter() stable {

  var count : Nat = 0;  // defaults to stable

  public func inc() : async Nat {
    count += 1;
    Prim.debugPrint (debug_show(count));
    count
  };

  // let f = func(){}; // rejected as unstable

}

Not sure I'm loving it, tbh.

github-actions[bot] commented 1 week ago

Comparing from 5e11c5de222f9102faa89bb7ccf19060cb32ee60 to a43782e35968f1b69feaf36b48958a74d217d090: The produced WebAssembly code seems to be completely unchanged.

ggreif commented 1 week ago

Wanna have some SQL feeling? actor Counter stableactor Counter with stable var 😜

crusso commented 15 hours ago

closing in favour of #4779