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
Demo: Generalizing the HashMap constructor, extract stable store
this is to demonstrate what I meant in
dfinity#300 (comment)
and
dfinity#299 (comment),
and how to introduce this without breaking changes (although it’s kinda
ugly)
What I would _want_ here is to introduce a second, more general,
constructor for the given class, but Motoko does not allow me to do that
easily. But I can hack around that by
* Creating a new class, not public `HashMap_` with the constructor I
want
* In `HashMap`’s constructor, call the `HashMap_` constructor to create
an inner object (`i`)
* In `HashMap`, simply copy all the fields from the inner objects to
the outer object.
* A public module-level function (here `wrapS`) exposes the new
constructor.
With this (generic, ugly) trick I can suppor the idiom
```
stable var userS : HashMap.S <UserId,UserData> = newS();
let user : HashMap.HashMap<UserId,UserData> = HashMap.wrapS(10, Nat.eq, Nat.hash, userS)
```
without changing the API.
But it is ugly, and the effect on documentation generation is probably
bad as well. So maybe a better course of action would be to have a midly
breaking change where we only have the “new” constructor, and people
will have to fix their code by passing `HashMap.newS()` as a new fourth
argument if they want their old behavior. Probably better than piling up
hacks like this.
In that case, simply rename `class HashMap_` to `HashMap`, remove `wrapS` and
the old `class HashMap`.
0 commit comments