Cool compiler error message with generics

Discussion in 'Java' started by HK, Jul 19, 2005.

  1. HK

    HK Guest

    import java.util.*;
    public class Bla<T> {

    private List<T>[] ary = new ArrayList[10];

    private class InnerClass<T> {
    public void boo() {
    List<T> et = ary[0];
    }
    }
    }

    Bla.java:8: incompatible types
    found : java.util.List<T>
    required: java.util.List<T>
    List<T> et = ary[0];


    It took me some time to realize that the <T> of
    InnerClass is a completely different one from
    the <T> of Bla. I wonder if the compiler should
    rather complain that type variable <T> is
    redefined at InnerClass<T>.

    Harald.
    HK, Jul 19, 2005
    #1
    1. Advertising

  2. On Tue, 19 Jul 2005 02:08:20 -0700, HK wrote:

    > It took me some time to realize that the <T> of
    > InnerClass is a completely different one from
    > the <T> of Bla. I wonder if the compiler should
    > rather complain that type variable <T> is
    > redefined at InnerClass<T>.


    Why, it is only consistent with fields, which may have the same name as
    well, but different types.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Jul 19, 2005
    #2
    1. Advertising

  3. HK wrote:
    > import java.util.*;
    > public class Bla<T> {
    >
    > private List<T>[] ary = new ArrayList[10];
    >
    > private class InnerClass<T> {
    > public void boo() {
    > List<T> et = ary[0];
    > }
    > }
    > }
    >
    > Bla.java:8: incompatible types
    > found : java.util.List<T>
    > required: java.util.List<T>
    > List<T> et = ary[0];
    >
    >
    > It took me some time to realize that the <T> of
    > InnerClass is a completely different one from
    > the <T> of Bla. I wonder if the compiler should
    > rather complain that type variable <T> is
    > redefined at InnerClass<T>.


    Obviously not a good idea to reuse generic parameter names in nested
    classes. I think the problem is defining lint warnings consistently that
    wouldn't complain about, say, having a local and a field with the same name.

    You can get something similar at runtime with misuse of class loaders.
    If the same class file is loaded by two loaders you can get a class cast
    exception claiming that you cannot cast com.mycorp.MyClass to
    com.mycorp.MyClass.

    Tom Hawtin
    --
    Unemployed English Java programmer
    Thomas Hawtin, Jul 19, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yan
    Replies:
    0
    Views:
    1,130
  2. Ram
    Replies:
    0
    Views:
    2,827
  3. Joseph Dionne

    Generics are cool

    Joseph Dionne, May 6, 2005, in forum: Java
    Replies:
    46
    Views:
    1,240
    Dale King
    Jun 1, 2005
  4. Tony Arcieri
    Replies:
    14
    Views:
    449
    Tony Arcieri
    Dec 9, 2010
  5. Tony Arcieri
    Replies:
    0
    Views:
    403
    Tony Arcieri
    Dec 14, 2010
Loading...

Share This Page