Motoko class self reference?

Is there a way to have a class have a self reference value to use for self referencing? When not using the class code sugar for type+func, and manually declaring the func, you can specify a reference like:

let a = object selfRef { .... }

and use `selfRef’ in the object code

Looks like docs has the definition for <id_this> as only in the object decl?

<shared-pat>? <sort>? class <id> <typ-params>? <pat> (: <typ>)? <class-body> :=
  type <id> <typ-params> = <sort> { <typ-field>;* };
  <shared-pat>? func <id> <typ-params>? <pat> : async? <id> <typ-args> =
    async? <sort> <id_this>? <obj-body>

Am I just missing something?

This should work:

class C() = self {
  public func f() { self.f() };
};
4 Likes