Interfaces, like those of Java

Is there a concept of interface in Motoko, in the sense of Java interfaces for example? I need a function to admit objects of different types but that they all have a common method implemented. Or how can I do this in Motoko?

Motoko uses structural typing so any object that implements methods of the expected signatures will satisfy an object type with methods of those signatures. It can also provide more members than required. I.e. just use an object type with the common members.

Motoko also supports bounded type parameters which can be used in more advanced scenarios.

On mobile, on vacation so can’t give examples.

3 Likes

Hello!! Thanks for the reply…
I think I’m understanding!!
If one of my classes is the following:

class hashable(){
func hash(){//function logic};
};

Could a function that accepts it have this form?

actor{
func hash(hash:() → Nat32): Nat32{
hash();
};
};

Thanks Claudio, there I was able to do what I needed. The below is just an example

1 Like