# Candid Modules?

**URL:** <https://forum.dfinity.org/t/candid-modules/15234>\
**Category:** Developers\
**Tags:** candid\
**Created:** [September 1, 2022, 6:58pm UTC](https://forum.dfinity.org/t/candid-modules/15234 "2022-09-01T18:58:28Z")\
**Posts on this page:** 16\
**Page:** 1

<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:** [September 1, 2022, 6:58pm UTC](https://forum.dfinity.org/t/candid-modules/15234/1 "2022-09-01T18:58:28Z")

</div>

I recently integrated a few third-party services and I think the story around doing that as it relates to Candid could be improved.

The current process seems to be to copy and paste from `README`s and `.did` files to your own `.did` file.

I think introducing a module system to Candid could provide a solution to ever-growing and unwieldy Candid files, as well as provide a better experience when integrating with third-party services.

@chenyan, I’m curious to know what you think.

## Proposal

Introduce an `import` keyword to Candid. Everything in a `.did` file would be implicitly exported.

e.g.

```auto
import canistergeek from "./canistergeek.did";

...

// Access types like canistergeek.LogMessagesData here if necessary

...

service : {
  ...
  collectCanisterMetrics: canistergeek.service.collectCanisterMetrics;
  getCanisterLog: canistergeek.service.getCanisterLog;
  getCanisterMetrics: canistergeek.service.getCanisterMetrics;
}

```

This would be backwards-compatible with how things work today.

## Alternatives Considered

To approximate the above proposal, service providers could follow a convention of separating the `service` declaration into a separate file from the rest, and then concatenating the files together using a custom build step.

Consumers would then add third-party `.did` files to the list of files they include in their own custom build step.

e.g. for service-providers:

- `methods.did` \<\> `service.did` = `canister.did`
- `types.did` \<\> `methods.did` \<\> `service.did` = `canister.did`
- `create.did` \<\> `read.did` \<\> `service.did` = `canister.did`
- …

e.g for consumers:

- `methods.did` \<\> `third-party-methods.did` \<\> `service.did` = `canister.did`

_Pros_

- Requires no changes to Candid; works today.

_Cons_

- Requires people to follow conventions; not be enforced by tooling.
- Requires a separate custom build step per-project.
- Still requires consumers to redeclare methods in their own `service` declaration.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [September 1, 2022, 7:11pm UTC](https://forum.dfinity.org/t/candid-modules/15234/2 "2022-09-01T19:11:40Z")

</div>

We actually have `import`: [candid/Candid.md at master · dfinity/candid · GitHub](https://github.com/dfinity/candid/blob/master/spec/Candid.md#imports), but it’s not scoped. All the type definitions are imported to the current scope.

---

<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:** [September 1, 2022, 7:15pm UTC](https://forum.dfinity.org/t/candid-modules/15234/3 "2022-09-01T19:15:50Z")

</div>

@chenyan thanks, I didn’t know that!

It sounds like `import` works a bit like the alternative approach I described, except it also includes `service`.

How does that work? Are all of the `service` definitions (including that of the consumer) merged? Are multiple imports/services supported?

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [September 1, 2022, 7:22pm UTC](https://forum.dfinity.org/t/candid-modules/15234/4 "2022-09-01T19:22:16Z")

</div>

It only imports the type definitions, the main service is ignored. Multiple imports are supported.

If you want to import each main service in the did file, you have to change the did file to make the main service a type definition as well:

```auto
service : { ... } => let my_service = service { ... }; 
                          service : my_service;

```

---

<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:** [September 1, 2022, 8:36pm UTC](https://forum.dfinity.org/t/candid-modules/15234/5 "2022-09-01T20:36:37Z")

</div>

It sounds like this supports the example I gave in my proposal.

I’ll give it a try and then try to socialize the approach and/or make PRs.

Thanks @chenyan!

---

<div class="post-metadata">

**Author:** ![rossberg](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/rossberg/32/795_2.png) [@rossberg](https://forum.dfinity.org/u/rossberg)\
**Post date:** [September 2, 2022, 8:03am UTC](https://forum.dfinity.org/t/candid-modules/15234/6 "2022-09-02T08:03:28Z")

</div>

> [@chenyan](#):
>
> It only imports the type definitions, the main service is ignored.

Interesting, I wasn’t aware of that – sounds a bit fishy :). An alternative would be to make trying to import something that has a service declaration an error. Of course, then people would have to split up their Candid sources into “libraries” and main files where reuse is intended, but that may be a good thing.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [September 2, 2022, 3:36pm UTC](https://forum.dfinity.org/t/candid-modules/15234/7 "2022-09-02T15:36:53Z")

</div>

Or we can introduce scoping as Paul suggested and import the main service as well. There is an open issue in the spec about whether we want to have qualified names for imports.

OTOH, `import` can only appear in local development. When we attach the did file as canister metadata, `import` cannot appear there, because there is no notion of file system. We can only dump the typing environment when exporting did file.

---

<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:** [January 11, 2023, 2:48am UTC](https://forum.dfinity.org/t/candid-modules/15234/8 "2023-01-11T02:48:57Z")

</div>

@chenyan I only just got around to trying this and I’m not sure I can do what I was hoping to do.

Is there a way to do something like this?

```auto
// canistergeek.did
...
type Canistergeek = service {
  collectCanisterMetrics: () -> ();
  getCanisterLog: (opt CanisterLogRequest) -> (opt CanisterLogResponse) query;
  getCanisterMetrics: (GetMetricsParameters) -> (opt CanisterMetrics) query;
};

```

```auto
// codebase.did
import "canistergeek.did";
...
service : () -> {
  ...
  collectCanisterMetrics: Canistergeek.collectCanisterMetrics;
  getCanisterLog: Canistergeek.getCanisterLog;
  getCanisterMetrics: Canistergeek.getCanisterMetrics;
}

```

or, even better would be:

```auto
// codebase.did
import "canistergeek.did";
...
service : () -> canistergeek && {
  ...
}

```

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [January 11, 2023, 3:33am UTC](https://forum.dfinity.org/t/candid-modules/15234/9 "2023-01-11T03:33:09Z")

</div>

You can do the last one but without the `&&` part: `service : () -> canistergeek`. We don’t have a way to project or extend the fields.

If you can refactor `canistergeek.did` to separate function types, then you can do the second one.

---

<div class="post-metadata">

**Author:** ![skilesare](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/skilesare/32/5609_2.png) [@skilesare](https://forum.dfinity.org/u/skilesare)\
**Post date:** [January 11, 2023, 4:36am UTC](https://forum.dfinity.org/t/candid-modules/15234/10 "2023-01-11T04:36:02Z")

</div>

{canistergeek with …} syntax won’t work?

---

<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:** [January 11, 2023, 4:37am UTC](https://forum.dfinity.org/t/candid-modules/15234/11 "2023-01-11T04:37:56Z")

</div>

That’s not valid Candid syntax AFAIK.

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [January 11, 2023, 4:43am UTC](https://forum.dfinity.org/t/candid-modules/15234/12 "2023-01-11T04:43:58Z")

</div>

`with` is a Motoko syntax, not Candid.

---

<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:** [January 11, 2023, 5:09am UTC](https://forum.dfinity.org/t/candid-modules/15234/13 "2023-01-11T05:09:08Z")

</div>

> [@chenyan](#):
>
> If you can refactor `canistergeek.did` to separate function types, then you can do the second one.

I did this for now:

```auto
type collectCanisterMetrics = func () -> ();
type getCanisterLog = func (opt CanisterLogRequest) -> (opt CanisterLogResponse) query;
type getCanisterMetrics = func (GetMetricsParameters) -> (opt CanisterMetrics) query;

```

The above works, but is it supported in `ic-repl`?

I have tests that do `import codebase = "<canister id>" as "candid/codebase.did";` that are now failing with `Error: Unbound type identifier collectCanisterMetrics`

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [January 11, 2023, 5:13am UTC](https://forum.dfinity.org/t/candid-modules/15234/14 "2023-01-11T05:13:59Z")

</div>

ic-repl assumes that the did file comes from the canister, so it ignores the import. But this is a good use case, I will fix it tomorrow.

---

<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:** [January 11, 2023, 9:53pm UTC](https://forum.dfinity.org/t/candid-modules/15234/15 "2023-01-11T21:53:58Z")

</div>

I created an issue for tracking purposes.

> <https://github.com/chenyan2002/ic-repl/issues/49>
>
> As discussed in https://forum.dfinity.org/t/candid-modules/15234/13?u=paulyoung
> …
> 
> @chenyan2002 said:
> 
> \> ic-repl assumes that the did file comes from the canister, so it ignores the import

---

<div class="post-metadata">

**Author:** ![chenyan](https://avatars.discourse-cdn.com/v4/letter/c/35a633/32.png) [@chenyan](https://forum.dfinity.org/u/chenyan)\
**Post date:** [January 11, 2023, 11:48pm UTC](https://forum.dfinity.org/t/candid-modules/15234/16 "2023-01-11T23:48:25Z")

</div>

Released: [Release 0.3.11 · chenyan2002/ic-repl · GitHub](https://github.com/chenyan2002/ic-repl/releases/tag/0.3.11) 🙂
