Skip to content

Add this as a self type - #1708

Open
HT154 wants to merge 10 commits into
apple:mainfrom
HT154:this-type
Open

Add this as a self type#1708
HT154 wants to merge 10 commits into
apple:mainfrom
HT154:this-type

Conversation

@HT154

@HT154 HT154 commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Depends on #1796

This PR:

  • Adds type this, a fully featured self type for use in modules or classes.
    • Forbids use of this in typealias bodies.
  • Deprecates usage of the module type in const scopes (class and typealias bodies, annotations, const properties/methods) with a warning, to be an error in a future release.
  • Adopts this in the standard library:
    • pkl.base#Any.getClass() now returns Class<this>.
    • pkl.base#Any.ifNonNull() now accepts an argument of type (this) -> Result.
    • pkl.ref#Domain.renderReference() now accepts a Reference<this, Any>.
  • This also a corner case with ReferenceTypeNode, module types, and type aliases that caused module types in the referent position to always be resolved to the module where the Reference type annotation is instead of where module is used.
  • Pipes a Logger through to AstBuilder so warnings/traces can be printed from it.

For ease of review, this PR consists of two commits; the first is all implementation changes, the second includes regenerated pkl-doc test output.

Resolves #1612
Resolves #1712

Comment thread pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java Outdated
Comment thread pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java Outdated
@HT154
HT154 force-pushed the this-type branch 2 times, most recently from 97dac9c to 7e94e96 Compare August 3, 2026 17:04
Comment thread pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java Outdated
Comment thread pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java Outdated
Comment thread pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java Outdated
Comment thread pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java Outdated
Comment thread pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java
Comment thread pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java Outdated
@HT154
HT154 requested review from bioball and stackoverflow August 13, 2026 20:49
@HT154
HT154 force-pushed the this-type branch 2 times, most recently from 01dd026 to 866dbc4 Compare August 14, 2026 18:09

@bioball bioball left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is getting close; did another pass.

One major comment here is: I think that we're missing a bunch of tests; there's a lot of positions in our grammar that can produce type nodes, which aren't tested.

Some of the ones that I can think of are:

  • Object body param (foo { bar: this -> /* etc */ })
  • Supertype (class Bar extends this {})
  • Function literal type annotation ((arg: this) -> ...)
  • For-generator variable
  • Let expr variable
  • Local object property new { local x: this = ... }
  • Object method arguments (new { local function foo(bar: this): this = ... })
  • Type cast expressions (foo as this)
  • Constraint property type (foo: this(...))
  • Function property type (foo: this -> this)
  • Listing<this> (test lazy check and default value behavior)

Comment thread pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java
@HT154
HT154 force-pushed the this-type branch 2 times, most recently from 1544122 to f73c4fd Compare August 19, 2026 16:01
@HT154
HT154 requested a review from bioball August 19, 2026 16:04
@HT154
HT154 force-pushed the this-type branch 3 times, most recently from b44b110 to afbf930 Compare August 19, 2026 18:26

@bioball bioball left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor nits and a test refactor, but otherwise LGTM!

Approving to unblock

Comment thread pkl-core/src/test/files/LanguageSnippetTests/input/types/thisType1.pkl Outdated
Comment thread pkl-core/src/test/files/LanguageSnippetTests/input/types/thisType1.pkl Outdated
Comment thread pkl-core/src/main/java/org/pkl/core/ast/builder/AstBuilder.java
Comment thread pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java Outdated
Comment thread pkl-core/src/main/java/org/pkl/core/ast/type/TypeNode.java Outdated
@HT154
HT154 force-pushed the this-type branch 2 times, most recently from b0806e0 to 2ad6f5d Compare September 1, 2026 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pkl is missing a self-type module types as typelias type args are evaluated in the context of the alias's module, not where module is used

3 participants