pkl icon indicating copy to clipboard operation
pkl copied to clipboard

Improve the usability of `outer`

Open HT154 opened this issue 4 months ago • 1 comments

cf. Peter's old notes

Currently, outer.foo literally accesses foo on outer. Instead, it should work more like super.foo, i.e., access the lexically closest foo, not necessarily on the directly enclosing object. Perhaps outer.foo should also work for method parameters and local properties, e.g. function myFunction(bar) = new { bar = outer.bar }

This is a potentially breaking change in cases like this:

class A {
  foo: String = "baz"
  b: B
}

class B {
  foo: String
}

foo: String = "bar"

a: A = new {
  b {
    foo = outer.foo
  }
}

In the current state, a.b.foo is "baz" since outer refers to a. As proposed, if outer.foo refers to the "lexically next outer" foo then a.b.foo is "bar".

HT154 avatar Aug 30 '25 22:08 HT154

This would definitely be a breaking change.

d = 5

a = (prop) {
 b {
   // currently, resolves to `6`. 
   // If changed to "lexically closest `d`", this lookup resolves to `5`.
   c = outer.d 
 }
}

prop {
  d = 6
}

I don't think this makes sense to change now. But, one paper cut here is that outer can only look up one level higher. Possibly, we should allow outer to be chained.

bioball avatar Sep 01 '25 05:09 bioball