A rule to make typing nested classes more flexible #14390
Replies: 1 comment
-
I meditated a bit on this example and now I'm no longer sure why does it matter that EDIT: ok, let me be a bit more explicit. If I look at the example, I already see |
Beta Was this translation helpful? Give feedback.
-
#14387 can replace some of #13657 but not all. For instance, the following would still require #13657 to be able to typecheck
tail
:But we can hopefully get on safer ground by adding an alternative rule. The idea is that the local class
Mapped
sees the outer referencesf
andxs
throughthis
. You could imagine a meaning preserving transformation wheref
andxs
are cached in additional fields of classMapped
like this:and every reference to
f
orxs
is replaced bythis.this_f
andthis.this_xs
.Let's now compare the expression
xs.tail.map(f)
of type{f, xs} LazyList[B]
with the expected type{this} LazyList[B]
. Since there are no tracked parameters enclosing the expression inside classMapped
, every occurrence off
orxs
in the capture setC
must have come throughthis
, so we can equivalently usethis.this_f
andthis.this_xs
, which are both subsumed bythis
. In other words, to compare actual{f, xs}
with expected{this}
, we can ignore any reference in the actual capture set that goes to a variable defined outsideMapped
, so bothf
andxs
are discarded and the check succeeds.More generally: When checking an expression
e: Ca Ta
against an expected typeCx Tx
where the capture set ofCx
containsthis
and any method inside the classCls
ofthis
that containse
has only pure parameters, drop fromCa
all references to variables orthis
references outsideCls
. These are all accessed throughthis
, so are already accounted for byCx
.Beta Was this translation helpful? Give feedback.
All reactions