Skip to content

Support self or this for referencing members #8765

@timotheeguerin

Description

@timotheeguerin

Problem

While doing this PR #8751 to fix some cases where we wrongly didn't instantiate types this uncovered a pattern that was kinda allowed before(though failing if you actually used it)

If an templated interface operation reference another operation in that interface then it would actually reference the interface declaration not the same instance. It somwhow kinda works when the parmaeter wasn't used but would get a crash as it would keep some TemplateParameter type in the type graph

playground

interface Base<A> {
  Default is Base.Custom<A>;

  Custom<T>(a: A): T;
}

interface Test extends Base<string> {}

Proposal

Introduce a self or this that reference to the current instance, the above could be rewritten as

interface Base<A> {
  Default is self.Custom<A>;

  Custom<T>(a: A): T;
}

Things to figure out what does it means with spread or inheritance like

model Base {
  id: string;
  extId: self.id;
}

model Case1 extends Base {
  id: uuid;
}

model Case2 {
  ...Pick<Base, "extId"> 
  id: uuid;
}

is extId string or uuid here

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler:coreIssues for @typespec/compilerdesign:neededA design request has been raised that needs a proposaltriaged:core

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions