# Wrapping a stable var

**URL:** <https://forum.dfinity.org/t/wrapping-a-stable-var/12763>\
**Category:** Developers\
**Created:** [May 6, 2022, 8:41am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763 "2022-05-06T08:41:19Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon98822045](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@anon98822045](https://forum.dfinity.org/u/anon98822045)\
**Post date:** [May 6, 2022, 8:41am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763/1 "2022-05-06T08:41:19Z")

</div>

We really need a way to skip the postupgrade/preupgrade checks as they’re taking up like 600 of our Wasm functions (out of the 6000 allowed), and creating a lot of code complexity. Also, life would be so much easier if things just persisted.

This appears to work but I’m not sure it will in practice. I’m using @icme’s StableRBTree library.

When using a non-OO library you have to replace the stable variable with each state change, and that’s not good when you have a complex OO system. I thought this may get around the problem.

```auto
  type dbType = RBT.Tree<Nat, Nat>;

  stable var characterDB : dbType = RBT.init<Nat, Nat>();

  type dbWrapper = {
    var db : dbType;
  };

  public shared({ caller }) func test() : async [(Nat, Nat)] {
    let w = {var db = characterDB};
    modifyDB(w);

    Iter.toArray(RBT.entries(w.db));
  };

  func modifyDB(w : dbWrapper) {
    w.db := RBT.put(w.db, Motoko.compareNat, 1, 33);
  };

```

---

<div class="post-metadata">

**Author:** ![justmythoughts](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/justmythoughts/32/5226_2.png) [@justmythoughts](https://forum.dfinity.org/u/justmythoughts)\
**Post date:** [May 6, 2022, 8:46am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763/2 "2022-05-06T08:46:48Z")

</div>

Looks like it is just a pointer update, not a full copy of the entire tree.

---

<div class="post-metadata">

**Author:** ![anon98822045](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@anon98822045](https://forum.dfinity.org/u/anon98822045)\
**Post date:** [May 6, 2022, 8:47am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763/3 "2022-05-06T08:47:05Z")

</div>

This may explain it a bit better

```auto
  type dbType = RBT.Tree<Nat, Nat>;

  stable var characterDB : dbType = RBT.init<Nat, Nat>();
  type dbWrapper = {
    var db : dbType;
  };

  public shared({ caller }) func test() : async [(Nat, Nat)] {
    var db = characterDB;
    let w = {var db = db};
    modifyWrapper(w);
    modifyDB(db);

    Iter.toArray(RBT.entries(w.db));
  };

  func modifyDB(db : dbType) {
    db := RBT.put(db, Motoko.compareNat, 1, 33);
  };

  func modifyWrapper(w : dbWrapper) {
    w.db := RBT.put(w.db, Motoko.compareNat, 1, 33);
  };

```

type error [M0073], expected mutable assignment target

---

<div class="post-metadata">

**Author:** ![anon98822045](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@anon98822045](https://forum.dfinity.org/u/anon98822045)\
**Post date:** [May 6, 2022, 8:47am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763/4 "2022-05-06T08:47:54Z")

</div>

yeah ill write a wrapper library that makes it easier to use in our system and that may give a few ideas. map/mapFilter for example.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [May 6, 2022, 9:07am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763/5 "2022-05-06T09:07:49Z")

</div>

> [@anon98822045](#):
>
> ```auto
> func modifyDB(db : dbType) {
> db := RBT.put(db, Motoko.compareNat, 1, 33);
> };
> 
> ```

Without line numbers of where the error is I’d say it’s happening here. Your input parameter is called db, and it’s not of type var so it can’t be mutated.

If you can point me to where the error is I can help you further

---

<div class="post-metadata">

**Author:** ![anon98822045](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@anon98822045](https://forum.dfinity.org/u/anon98822045)\
**Post date:** [May 6, 2022, 9:10am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763/6 "2022-05-06T09:10:09Z")

</div>

Yeah sorry I know why the error was occurring, I was just showing that wrapping the mutable variable allows us to pass it through functions and classes. That was the bit I was missing before.

---

<div class="post-metadata">

**Author:** ![icme](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/icme/32/4327_2.png) [@icme](https://forum.dfinity.org/u/icme)\
**Post date:** [May 6, 2022, 9:11am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763/7 "2022-05-06T09:11:10Z")

</div>

Yep, as long as you wrap it in a stable type like a record you should be fine.

Just curious though, why do you need to wrap the db?

---

<div class="post-metadata">

**Author:** ![anon98822045](https://avatars.discourse-cdn.com/v4/letter/a/ce73a5/32.png) [@anon98822045](https://forum.dfinity.org/u/anon98822045)\
**Post date:** [May 6, 2022, 9:13am UTC](https://forum.dfinity.org/t/wrapping-a-stable-var/12763/8 "2022-05-06T09:13:52Z")

</div>

![image](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/2X/d/db8d0d68ceb6b10a24c87c0a6a6968cc79e71699.png)

because we’ve got a OO ORM with transactions, indexes and all sorts of stuff and I couldn’t imagine how to build that without passing the dbs as arguments
