Writing Motoko stable libraries

About the class+, I gotta say I never understood why this hasn’t become more of a standard. It gradually became the goto in my codebase. I remember asking the question earlier this year: What prevents me from wrapping stable types from the Motoko base library in a OOP way using a ref on the stable type?

You can wrap pretty much everything in a class+. I use a (quite ugly) type for simple variables:

  public type Ref<V> = {
    var v: V;
  };

that I initialize in stable memory and then “inject” in my class+.
I think the downside is that you cannot make it const, sometimes I’d like to have a class that reads the value but is not able to modify it.

1 Like