Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a tool to extract actor or class signatures #4753

Open
vporton opened this issue Nov 4, 2024 · 6 comments
Open

Need a tool to extract actor or class signatures #4753

vporton opened this issue Nov 4, 2024 · 6 comments

Comments

@vporton
Copy link

vporton commented Nov 4, 2024

We need to have a tool that will extract actor/class signatures to create "header" files. I will explain on an example:

actor class A() {
  public shared func f() {};
}

would be transformed into:

module A {
  type A = actor {
    f: shared () -> ();
  };
}
@rvanasa
Copy link
Contributor

rvanasa commented Nov 5, 2024

Thank you for the suggestion! Is there a motivating project / use case so we can better understand the problem you hope to solve with this tool?

@vporton
Copy link
Author

vporton commented Nov 5, 2024

Thank you for the suggestion! Is there a motivating project / use case so we can better understand the problem you hope to solve with this tool?

Of course: I have a CanDB partition actor X dependent on actor Y. But Y needs to call methods from X. I can't make Y dependent on X, because it would be a circular dependency (and this also would greatly increase compilation time). So I would preprocess X to have an abstract actor to include into Y.

Currently I edit this abstract actor manually what is error-prone.

@rvanasa
Copy link
Contributor

rvanasa commented Nov 5, 2024

If I understand correctly, we currently have a way to do this with Motoko bindings from dfx generate. Here is an example dfx configuration which illustrates how this works in practice.

If you want a fully custom solution, you could look into using file.parseMotokoTyped() from the motoko npm package to process the AST of the Motoko actor in any way that you'd like.

@vporton
Copy link
Author

vporton commented Nov 6, 2024

This does not work in practice:

The following is a simplified idea of mutual dependence: in one direction dependency on the implementation and in other one just on the interface.

Consider A.mo depending on B.mo in implementation and B.mo dependent on A.mo as an interface:

dfx.json:

{
  "canisters": {
    "A": {
      "main": "src/interdep_backend/A.mo",
      "type": "motoko",
      "dependencies": ["B"],
      "declarations": {
        "bindings": ["mo"]
      }
    },
    "B": {
      "main": "src/interdep_backend/B.mo",
      "type": "motoko"
    }
  },
  "defaults": {
    "build": {
      "args": "",
      "packtool": ""
    }
  },
  "output_env_file": ".env",
  "version": 1
}

A.mo:

import B "canister:B";

actor A {};

B.mo:

import A "../declarations/A/A";

actor B {};

This is impossible to compile:

To compile B.mo, need first create declarations/A/A.mo:

$ dfx generate A
...
/home/porton/Projects/bug/interdep/src/interdep_backend/B.mo:1.1-1.31: import error [M0009], file "/home/porton/Projects/bug/interdep/src/declarations/A/A/lib.mo" does not exist

That is, generation of A depends on itself.

Any ideas how to fix it?

@rvanasa
Copy link
Contributor

rvanasa commented Nov 7, 2024

I asked about this internally, and it seems like there isn't really a way around this given the design of dfx. The cleanest solution would be to remove the mutual dependency if possible. I'll mention this in the next Languages team meeting so we can decide on a priority for addressing this, perhaps via a header module code generation tool as you suggested.

Note that it would be a while before this is available (at least several months, possibly over a year) if we end up building this due to prioritizing items on the Motoko roadmap.

@crusso
Copy link
Contributor

crusso commented Nov 7, 2024

Not that I'm recommending this approach but I did actually come up with a workaround for this using hand-crafted candid for the forward declarations.

https://github.com/crusso/circ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants