August 22, 2007

Helping javac infer generic return types

One of my pet peeves with Java generics is that you can't pass the result of a method that returns a generic type whose bounds cannot be inferred from actual arguments directly to another method. This is particularly useful when passing an empty list obtained from Collections.emptyList() to a method:

class Test
{
   static void foo(List<String> list) {}

   public static void main(String[] args)
   {
     foo(Collections.emptyList()); // doesn't compile
   }
}


Unfortunately, that doesn't compile. It can be worked around by doing:

List<String> list = Collections.emptyList();
foo(list);


It beats me why the compiler cannot infer the appropriate type for the list returned by Collections.emptyList() in the first example above. I understand why it would fail when 'foo' has multiple overloads, but it should work for the simple case when the method resolution is unambiguous.

I recently came across an obscure (yes, ugly and unintuitive) way of getting the first example to work. It consists in telling the compiler explicitly what to bind the return type to. This is the BNF definition, per the JLS 3.0:

MethodInvocation:
   MethodName ( ArgumentList? )
   Primary . NonWildTypeArguments? Identifier ( ArgumentList? )
   super . NonWildTypeArgumentsopt Identifier ( ArgumentList? )
   ClassName . super . NonWildTypeArguments? Identifier ( ArgumentList? )
   TypeName . NonWildTypeArguments Identifier ( ArgumentList? )


And here's a concrete example:

foo(Collections.<String>emptyList());

Labels: ,

28 Comments:

Blogger e-BN said...

The compiler rejects the first call because it does not have enough information to infer a type for the type variable 'T' [List<T> emptyList()]. Since the method emptyList does not take any parameters, no constraints are collected that could help compute a type for T. In such case, the compiler turn the call's context. However, in this situation there also no information available from the context, because the call is not being used in an assignment. So, the compiler choses in this case to infer the default type Object for T. This gives you a List<Object> back, which is unfortunately not a subtype of foo's parameter List<String>.

So aside from a parameterized call, you could also assign the result of emptyList() to a local variable and then call foo with the local variable as a parameter.

January 07, 2008 3:00 PM  
Blogger dailymoney said...

Free softwares

http://dailyhotsoftwares.blogspot.com/

September 23, 2008 9:59 AM  
Blogger joginder singh punia said...

For learning and finding a job in .NET,JAVA,SQL,PHP,HTML,FLASH,SEO,TALLY,C++,C#,XML,DREAMVIEWR SOFTWARE,PHOTOSHAP,MS OFFICE AN IN IT SECTOR u can take help from www.softwareitjob.com also for preparing for interview for a job

December 14, 2008 7:50 AM  
Blogger joginder singh punia said...

r u searching a job in software or IT sector? r u preparing for interview in any field? do u want to learn .NET,JAVA,SQL,PHP,HTML,FLASH,SEO,TALLY OR ANY OTHER SOFTWARE ? for finding answer of all these questions go to www.softwareitjob.com www.matavaishno.com

March 21, 2009 10:42 PM  
Anonymous Anonymous said...

Glad I stumbled into this article! Finally, got what I was looking for to put on my school report... Thanks for sharing this. Please come visit my site Bridgeport Connecticut yellow pages when you got time.

July 21, 2009 11:22 PM  
Blogger pimpinkicks said...

I usually don’t leave comments!!! Trust me! But I liked your blog…especially this post! Would you mind terribly if I put up a backlink from my site to your site? Please come visit my site State of New Jersey when you got time.

July 29, 2009 12:22 AM  
Blogger pimpinkicks said...

I enjoyed reading your work! GREAT post! I looked around for this… but I found you! Anyway, would you mind if I threw up a backlink from my site? Please come visit my site State of New Mexico when you got time.

July 29, 2009 12:29 AM  
Anonymous Anonymous said...

Really great work. Congrats to everyone who are involved with this project. The website layout and graphics are really cool. Please come visit my site Shreveport Business Directory when you got time.

July 29, 2009 5:37 AM  
Anonymous Anonymous said...

Awesome! I have read a lot on this topic, but you definitely give it a good vibe. This is a great post. Will be back to read more! Please come visit my site Long Beach Business Directory when you got time.

July 29, 2009 5:38 AM  
Blogger pimpinkicks said...

You may have not intended to do so, but I think you have managed to express the state of mind that a lot of people are in. The sense of wanting to help, but not knowing how or where, is something a lot of us are going through. Please come visit my site State of New Jersey when you got time.

August 14, 2009 3:43 AM  
Blogger pimpinkicks said...

You may have not intended to do so, but I think you have managed to express the state of mind that a lot of people are in. The sense of wanting to help, but not knowing how or where, is something a lot of us are going through. Please come visit my site State of New Jersey when you got time.

August 14, 2009 3:45 AM  
Blogger pimpinkicks said...

I am not really sure if best practices have emerged around things like that, but I am sure that your great job is clearly identifed. I was wondering if you offer any subscription to your RSS feeds as I would be very interested and can’t find any link to subscribe here. Please come visit my site South Valley. when you got time.

August 14, 2009 3:47 AM  
Anonymous Anonymous said...

love your blog! Very cool! Please come visit my site lupus symptoms when you got time.

October 02, 2009 7:06 AM  
Anonymous Anonymous said...

You owe a very nice and interesting blog. Please
visit my site post traumatic stress disorder when you got time.

October 02, 2009 7:07 AM  
Anonymous Anonymous said...

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog? Please come visit my site Business Trade Guide of San Diego California CA when you got time.

October 17, 2009 8:43 PM  
Anonymous Anonymous said...

Wow! Thank you! I always wanted to write in my site something like that. Can I take part of your post to my blog? Please come visit my site San Diego Business Services And Classifieds when you got time.

October 17, 2009 8:44 PM  
Anonymous Anonymous said...

Such interesting read and information, thanks for sharing this post. I will check back to read your other new posts. Please come visit my site Directory Assistance Charlotte when you got time.

October 25, 2009 3:08 AM  
Anonymous Anonymous said...

Such interesting read and information, thanks for sharing this post. I will check back to read your other new posts. Please come visit my site American Charlotte online business directory when you got time.

October 25, 2009 3:08 AM  
Blogger The Jack said...

Thanks ever so much, very useful article. If you do not mind, please visit my article related to pandeglang district in Banten, Indonesia at Kenali dan Kunjungi Objek Wisata di Pandeglang or Kenali dan Kunjungi Objek Wisata di Pandeglang second and also Kenali dan Kunjungi Objek Wisata di Pandeglang Objek Wisata Air Terjun Curug Gendang or related to a leadership at Mengembalikan Jati Diri Bangsa and Oes Tsetnoc or Oes Tsetnoc the second and our hard work at Kerja Keras Adalah Energi Kita that's right Kerja Keras Adalah Energi Kita, and Kenali dan Kunjungi Objek Wisata di Pandeglang Memasuki Babak Akhir also Objek Wisata Taman Wisata Alam Carita, Kenali dan Kunjungi Objek Wisata di Pandeglang, or Kenali dan Kunjungi Objek Wisata di Pandeglang, also Kenali dan Kunjungi Objek Wisata di Pandeglang, or Kenali dan Kunjungi Objek Wisata di Pandeglang, also Kenali dan Kunjungi Objek Wisata di Pandeglang, or Kenali dan Kunjungi Objek Wisata di Pandeglang, also Kenali dan Kunjungi Objek Wisata di Pandeglang, or Kenali dan Kunjungi Objek Wisata di Pandeglang, very smart thank you!

October 28, 2009 11:05 PM  
Anonymous Anonymous said...

I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business Thanks for all your help and wishing you all the success in your business Please come visit my site Norfolk Business Services And Classifieds when you got time.

November 02, 2009 1:22 AM  
Anonymous Anonymous said...

I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business Thanks for all your help and wishing you all the success in your business Please come visit my site Virginia VA Phone Directory when you got time.

November 02, 2009 1:23 AM  
Anonymous Anonymous said...

hello~nice to meet u.........................................

December 03, 2009 4:49 PM  
Anonymous Anonymous said...

Nice work and thanks!
Running
Adidas currently manufactures several running shoesNike shoes, including the adiStar Control 5, the adiStar Ride
Cheap nike shoes
Discount nike shoes
the Supernova Sequence and the Supernova Cushion 7, among others.
Nike shox r4
nike shox shoes
Adidas also uses kangaroo leather to make their more expensive shoes.
Association football
One of the main focuses of Adidas is football kit and associated equipment.
puma cat
cheap sport shoes
Adidas also provides apparel and equipment for all teams in Major League Soccer. Adidas remain a major company in the supply of team kits for international football teams.
cheap nike shox
cheap nike max
Adidas also makes referee kits that are used in international competition and by many countries and leagues in the world. In the United States, referees wear the Adidas kits in MLS matches even though the primary referee supplier is Official Sports.
nike tn dollar
nike running shoes
The company has been an innovator in the area of footwear for the sport with notable examples including development of the Copa Mondial moulded boot on firm dry pitches for forty years.
nike air max tn
puma shoes
Adidas became renowned for advancing the "Predator" boot design.This design featured a ribbed rubber structure for the upper leather of the shoe, used to accent the movement of the ball.
discount puma shoes
puma mens shoes
The Predator also features the Craig Johnston invented "Traxion" sole. As the development and popularity of Football continued Adidas played a leading role in shaping the style of the play itself.
puma running shoes
puma shoes
FIFA, the sports governing body, commissioned specially designed footballs for use in its own World Cup tournaments to favour more attacking play.
ghd hair straighteners mk4
hair straightners
ghd iv styler hair straightener
ghd hair straightners
cheap ghd hair straighteners

December 08, 2009 6:53 PM  
Blogger rr8004 said...

Yeah it is a great and nice article looking forward to have such article it is so useful. Please come visit my site Business Trade Guide of Laredo Texas TX when you got time.

December 27, 2009 10:55 PM  
Blogger rr8004 said...

Yeah it is a great and nice article looking forward to have such article it is so useful. Please come visit my site Laredo City Business Listings when you got time.

December 27, 2009 10:55 PM  
Blogger rr8004 said...

Couldn't be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing! Please come visit my site Sacramento Phone Book when you got time.

January 06, 2010 2:06 AM  
Blogger Unknown said...

Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post. Please come visit my site Business Trade Guide of Scottsdale Arizona AZ when you got time.

January 11, 2010 1:41 AM  
Blogger Unknown said...

Me and my friend were arguing about an issue similar to this! Now I know that I was right. lol! Thanks for the information you post. Please come visit my site Directory Scottsdale City when you got time.

January 11, 2010 1:42 AM  

Post a Comment

<< Home