Could we add a lint warning for when the type parameter name overloads an existing type name?

Archie Cobbs archie.cobbs at gmail.com
Mon Apr 15 16:19:02 UTC 2024


Replying only to amber-dev for now...

I agree this seems like it would be an improvement. A slight variant of the
problem that I've witnessed cause confusion multiple times is this (boiled
down):

public class MyClass<T> {
    public <T> void foo() {
        // easy to confuse what "T" means here
    }
}

Java's decision to make the shadowing of normal variables illegal was a
nice "advance" at the time, and I've always wondered why it shouldn't carry
over to generic type variables as well. It seems like the same basic
principle would apply.

Or maybe not?

Generic type variables are a kind of combination of "type name" and
"variable". While Java doesn't allow shadowing for variables, it does for
type names - for example:

public class MyClass<T> {
    public class String {   // no error here
    }
    public class T {        // no error here
    }
}

So maybe Java was just making the conservative choice at the time.

-Archie

On Sun, Apr 14, 2024 at 10:33 PM David Alayachew <davidalayachew at gmail.com>
wrote:

> In the vein of smoothing the on-ramp for beginners, one of the biggest
> pain points I have found when tutoring beginners is when they start to
> learn generics, and then do something like this.
>


-- 
Archie L. Cobbs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240415/7301fd39/attachment.htm>


More information about the amber-dev mailing list