Type parameters inside super() calls?
Maurizio Cimadamore
maurizio.cimadamore at oracle.com
Thu Feb 2 10:12:32 UTC 2023
On 02/02/2023 01:04, Alex Buckley wrote:
> But you are saying this is "static context overreach", and that
> perhaps `new Local()` should be allowed there. Such an expression
> needs to use `this` as the immediately enclosing instance of the Local
> object, and that's fine as long as no-one can access the immediately
> enclosing instance ... but now we invoke a method on the Local object
> and ????
I realized my local class example was borked in some ways, and not in
others.
It is borked because when you do `new Local()` you need an enclosing
instance (`this`), but you have not yet ran `super`, so that's a clear
issue (same accessing an instance method).
But, should the _type_ Local be unavailable? What if I do `(Local)null`
- is that an error? Today it will be - but here the expression is not
using `this` in any way.
What irks me is that using the "static context" trick biases us on which
_types_ we can and cannot see, while the whole point of the restrictions
is to prevent initialization bugs (e.g. use `this` before `super` is
called) which involve _expressions_ not _types_.
Because of that, I find the type-variable restriction too over-reaching
- if the only way to sensibly use a type-variable was by using `this`,
fine, it wouldn't matter in practice. But a constructor can accept an
argument whose type is a type-variable - to manipulate that type you
never need to use `this`. And, more concretely, let's say you need to
call a _static_ generic method and pass that argument (some T), and
inference fails, then the user, with the current restrictions, has no
way to "fix" the inference error by providing a concrete <T> witness.
Again, I don't expect this to come up much, but it's one of those things
that seems somewhat arbitrary.
Maurizio
More information about the compiler-dev
mailing list