Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Cool compiler error message with generics

Reply
Thread Tools

Cool compiler error message with generics

 
 
HK
Guest
Posts: n/a
 
      07-19-2005
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.

 
Reply With Quote
 
 
 
 
Stefan Schulz
Guest
Posts: n/a
 
      07-19-2005
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"


 
Reply With Quote
 
 
 
 
Thomas Hawtin
Guest
Posts: n/a
 
      07-19-2005
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
Generics are cool Joseph Dionne Java 46 06-01-2005 03:08 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments