Motoko Sharable Generics

I’m trying to generalize some watch/notify functionality into a module. I have the following def:

public type Event<X>  = {
            #Create : X;
            #Mutate : X;
            #Delete : X;
    };
public type Notify<X> = shared (Event<X>) -> ();

However, the compiler doesn’t like this

type error [M0031], shared function has non-shared parameter type

Understandable. But, I expected the compiler to instead give me an error at the time of implementing a truly non-shared “Event” for Notify. Which is likely easier said than done.

Is there a good away around this?

1 Like

Not currently, I’m afraid. We have plans to add shared generic types in the future, so that you could write

type Notify<shared X> = shared (Event<X>) -> ()

But unfortunately, that is highly non-trivial to implement for generic functions or classes (as opposed to just types), because it will require constructing and passing types around at runtime. So I fear it will take a bit longer.

3 Likes