Motoko Actor Class Implements API Interface

Given all of the different ICRC and API standards that are coming out, it would be nice to have an explicit syntax saying that an actor class implements those API standards, and then have that syntax enforce that the API type signature is implemented and correct in an actor.

Then someone can build an “InterfaceStandards” module, and developers can import the standard types, and use them in their actors.

Right now, I can do this:

actor class MyCanister : async (Interfaces.ICRC1 and Interfaces.ICRC2 and ICRC64) {
  ...
}

It’s ok, but doesn’t ready as clearly. A cleaner syntax for this might looks something like:

actor class MyCanister implements Interfaces.ICRC1, Interfaces.ICRC3, Interfaces.ICRC64 {
 ...
}
4 Likes

Cleaner by what metric? Personally, I would argue that the former is much cleaner because it is regular wrt to Motoko’s type language and does not involve inventing new syntax that is both redundant and ad-hoc (nor shorter). :wink:

1 Like

Respectfully, (as a software engineer and not a programming languages expert) I disagree that

actor class X : async (T1 & T2 & T3 & ...Tn) { ... }

is a cleaner API than

actor class X implements T1, T2, T3, ...TN { ... }

The only reason I found out about the currently available solution was by directly asking the Motoko team. The lack of a keyword or documentation of this functionality probably doesn’t help in that regard.

I’m not quite sure how to quantify a syntax other than to say classes (or in this case class actors) implementing an interface is a feature that is already present in many commonly used languages, including the one that Motoko models much of its syntax after, TypeScript.


A few additional points:

  • Actors already have the class syntax, and most languages with classes have an implements keyword to ensure the class implements a specific API contract. This analogy plays very well with the standard interfaces
  • After ICRC-1 and ICRC-2, many ICRCs and canister API interfaces are being developed right now by working groups to enable reliable inter-canister interaction. As an actor based language developed specifically for the Internet Computer, Motoko should provide a smooth and intuitive syntax for ensuring actors implement the correct and expected API interfaces.
1 Like

The second one is certainly more explicit. I love that the second one works and will plug it into my icrc examples.

I’m assuming that this will work for classic actor interfaces , but also guessing I can’t tag my standard classes with shared interfaces as mentioned in the other thread…hopefully we can get those partial actors at some point!

@icme, you seem to be equating cleanness with familiarity or proliferation, which I would object to (by that measurement, C++ and JS would be clean languages :slight_smile: ).

I count as “clean” what expresses analogous semantics with analogous syntax, and where analogous syntax implies analogous semantics. In particular, classes are merely a particular form of functions in Motoko. As a tiny thought experiment, would you imagine writing

func f() implements Int { ... }

for regular functions?

No doubt the documentation and discoverability could be improved, of course.