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

David Alayachew davidalayachew at gmail.com
Mon Apr 15 21:27:15 UTC 2024


Hello Archie,

Thank you for your response!

> 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
>     }
> }

Yeah, I have been bitten by this one too. I would very much like it if this
also got bundled into the warning concept that I proposed.

Shadowing is useful since it facilitates migration, but it has the sharp
edge of making future updates a little more error-prone. Leaving it as is
might make sense, but those who want it would likely appreciate a guard
rail in the form of a warning.

> Java's decision to make the shadowing of normal variables illegal...

I'm actually confused what you mean here. When you say shadowing of normal
variables, I am interpreting something like the following example.

public class tuv
{

    int x = 0;

    public void someMethod()
    {

        int x = 2;

        System.out.println(x); //prints 2

    }

    public void anotherMethod()
    {

        System.out.println(x); //prints 0

    }

}

But the above example is legal Java. Could you clarify?

Since the rest of your email builds on that point, I'm not able to follow
along.

Thank you for your help!
David Alayachew


On Mon, Apr 15, 2024 at 12:19 PM Archie Cobbs <archie.cobbs at gmail.com>
wrote:

> 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/b0ca4bfe/attachment-0001.htm>


More information about the amber-dev mailing list