Actor self reference in initialization values

Is there a good way to self reference the actor id during initialization of values?

shared actor class TeamActor() : async Types.TeamActor = this {

  stable let team = TeamState.TeamState(Principal.fromActor(this)); // Error: `cannot use this before this has been defined`

}
3 Likes

Check your message for help

My error message? Not sure if i understand

This is an annoying restriction. We are trying to lift it but for the moment, your best bet, if you can make it work, is to pass the principal in as an argument to the class, or lazily initialize it from a shared function, where this is considered defined.

Related issues:

1 Like

Programmatic import in Motoko - #17 by claudio links to an old PR that contains an example of doing this.

1 Like