Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

inconsistent compiler behavior with generics example

Thread view: 
Roger Levy - 03 Sep 2007 19:33 GMT
I have this new function that I wrote inside Eclipse (OS X, Java
1.5.0) and it both compiles and runs in Eclipse, and compiles using
javac within OS X.  But it fails to compile through either (1) javac
on Linux (version 1.6.0), or (2) Apache ant on either OS X or Linux.
Here's the program and I'm appending the compile-time error message.

Thanks

Roger

***

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class Blah {

 public static <E,L1 extends Collection<E>, L2 extends
Collection<L1>> L2  asListOfLists(E[][] array) {
   List<List<E>> result = new ArrayList<List<E>>();
   for(E[] a : array ) {
     result.add(Arrays.asList(a));
   }
   return (L2) result;
 }

 public static void main(String[] args) {
   System.out.println(asListOfLists(new Integer[][] { { 1,2 },
{ 3,4 }} ));
 }

}

Error:

$ javac -d . Blah.java
lBlah.java:19: incompatible types; inferred type argument(s)
java.util.Collection<java.lang.Integer>,java.lang.Object do not
conform to bounds of type variable(s) L1,L2
found   : <L1,L2>L2
required: java.lang.Object
   System.out.println(asListOfLists(new Integer[][] { { 1,2 },
{ 3,4 }} ));
                                   ^
Note: Blah.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Lew - 03 Sep 2007 19:55 GMT
> I have this new function that I wrote inside Eclipse (OS X, Java
> 1.5.0) and it both compiles and runs in Eclipse, and compiles using
[quoted text clipped - 45 lines]
> Note: Recompile with -Xlint:unchecked for details.
> 1 error

- Autoboxing and arrays don't mix.
- Generics and arrays don't mix.
- You cannot declare an array of genericized type, really.
- You cannot cast to a genericized type, really.

Instead of relying on autoboxing, which notoriously doesn't treat Integer[]...
well at all, declare the constants directly as the object type and see what
happens.

Integer.valueOf(1), not 1.

Then abandon the idea of directly creating a genericized array, or of casting
to the generic type.

Signature

Lew

Piotr Kobzda - 04 Sep 2007 12:36 GMT
[...]

>   public static <E,L1 extends Collection<E>, L2 extends
> Collection<L1>> L2  asListOfLists(E[][] array) {
[quoted text clipped - 4 lines]
>     return (L2) result;
>   }

Regardless of what your compilers are saying, here is incompatibility of
 local 'result' type and the method's return type (L2) -- List<List<E>>
_is not_ compatible with each type that is a Collection<Collectoion<E>>,
it's not even compatible with each type compatible with List<List<E>>,
for instance, cast to LinkedList<LinkedList<E>> will fail here.

Try the following:

    public static <E> List<List<E>> asListOfLists(E[][] array) {
        List<List<E>> result = new ArrayList<List<E>>();
        for(E[] a : array) {
            result.add(Arrays.asList(a));
        }
        return result;
    }

piotr
Roger Levy - 04 Sep 2007 16:31 GMT
> [...]
>
[quoted text clipped - 12 lines]
> it's not even compatible with each type compatible with List<List<E>>,
> for instance, cast to LinkedList<LinkedList<E>> will fail here.

Ah, yes, of course you're right.  I was thinking about this
improperly.  Thank you!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.