# Candid support for generics

**URL:** <https://forum.dfinity.org/t/candid-support-for-generics/12143>\
**Category:** Developers\
**Created:** [April 14, 2022, 7:45am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143 "2022-04-14T07:45:51Z")\
**Posts on this page:** 12\
**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:** [April 14, 2022, 7:45am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/1 "2022-04-14T07:45:51Z")

</div>

Hi, we’re running into a few issues with Dragginz and wanted the community to do a quick sanity check.

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

We’re building a feature-rich ORM with ACL rules, indexes, tree structures, validation rules, etc. However we can’t get around the fact that even though the library code relies heavily on generics, the front end can’t.

It’s weighing in at about 20,000 lines of motoko code and 148 entities (80 of which have TrieMaps to store data). By the time we’re at MVP it’s probably going to be closer to 300 entities and 40k lines of code. A lot of this code is generated by Go so it’s really easy to make structural changes. (sorry, @Gabriel)

It’s not a huge issue really. Compile time is about 2 minutes 15 seconds, which is annoying. If I get rid of all the Main.mo shared functions then it’s 36 seconds.

Wondered if anybody had any input!

---

<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:** [April 14, 2022, 7:51am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/2 "2022-04-14T07:51:37Z")

</div>

as an aside, if anybody’s curious here’s one of the 80 database classes with indexing

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

and here’s one that has a data transformer that takes a parentID and turns it into a nested set implementation

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

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [April 14, 2022, 8:16am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/3 "2022-04-14T08:16:34Z")

</div>

Related: [Feature Idea: Generic data · Issue #245 · dfinity/candid · GitHub](https://github.com/dfinity/candid/issues/245)

---

<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:** [April 14, 2022, 8:31am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/4 "2022-04-14T08:31:31Z")

</div>

Wow, above my pay grade… but implementing this would be great for our project.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [April 14, 2022, 10:09am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/5 "2022-04-14T10:09:48Z")

</div>

I think you can remove a lot of code by using a variant where you might imagine you’d want a generic type argument.

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [April 14, 2022, 10:20am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/6 "2022-04-14T10:20:45Z")

</div>

Does this approach improve things at all?

```auto
type EntityType = {
  …
  #zone;
};

```

```auto
type Entity = {
  …
  #zone : E.Zone;
};

```

```auto
public shared query({ caller }) func loadMany(entity : EntityType, ids : [Nat]) : async (Result, [Entity]) {
  switch entityType {
    …
    case (#zone) {
      #zone Query<R.Zone, M.Zone>(caller, db.zone, "root.admin.load").
      loadMany(ids);
    }
  };

```

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [April 14, 2022, 10:24am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/7 "2022-04-14T10:24:43Z")

</div>

Actually, since `[Entity]` is being returned you’d need to use `map` to wrap everything in the array with `#zone`

---

<div class="post-metadata">

**Author:** ![paulyoung](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/paulyoung/32/5251_2.png) [@paulyoung](https://forum.dfinity.org/u/paulyoung)\
**Post date:** [April 14, 2022, 10:26am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/8 "2022-04-14T10:26:53Z")

</div>

…or have a version of `Entity` called `Entities` where `#zone : [E.Zone]` and return `Entities` instead.

---

<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:** [April 14, 2022, 10:40am UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/9 "2022-04-14T10:40:04Z")

</div>

This could work actually, I have to look into it because the rest of the code is pretty convoluted… but if each front end function was a massive switch then it may be a lot faster.

I think a lot of the compile time comes from the public actor functions as Main.mo is about 20% of the overall codebase but 80% of the compile time.

Will give it a go and let you know! Thanks

---

<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:** [April 14, 2022, 6:10pm UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/10 "2022-04-14T18:10:24Z")

</div>

I think this is actually too much of a recode right now.

Maybe it’s worth it to get the compile time down, but the issue is we have index lookup functions that also pass generic fields. Doesn’t seem a clear path to having a cleaner code base long term.

It is what it is I guess, shouldn’t really complain that much!

---

<div class="post-metadata">

**Author:** ![nomeata](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/nomeata/32/733_2.png) [@nomeata](https://forum.dfinity.org/u/nomeata)\
**Post date:** [April 17, 2022, 5:13pm UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/11 "2022-04-17T17:13:44Z")

</div>

I’m a bit confused: Is this thread about bad Motoko compiler performance (which could be looked into, I expect a lot of low hanging fruit there) or missing language features in Candid?

---

<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:** [April 17, 2022, 5:42pm UTC](https://forum.dfinity.org/t/candid-support-for-generics/12143/12 "2022-04-17T17:42:41Z")

</div>

Well both really, but the latter is more important for us.

It was a case of “we can’t do this so let’s generate a load of code, and we think that extra code is slowing down our compile time.”

Turns out the compile time was caused by a public TrieMap on our database store class. Once we made it private and restricted access to the methods the compile time halved.

So yes, we’d love candid generics, but only because it simplifies our codebase.
