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: ,

49 Comments:

Blogger Hitt 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 tercumenette said...

nicepost

March 27, 2008 12:55 PM  
Blogger dailymoney said...

Free softwares

http://dailyhotsoftwares.blogspot.com/

September 23, 2008 9:59 AM  
Blogger ecorange said...

ネットショップ 構築

December 08, 2008 3:02 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  
Blogger nasha said...

Why was there no follow on bankruptcy then? The bailout of AIG FP went to (wow power leveling) hedge funds that bound credit swaps on Lehman failing or others betting on rating (wow power leveling) declines. AIG has drained over 100 billion from the government. Which had to go to (wow power leveling) those who bet on failures and downgrades. Many of whom (power leveling)were hedge funds. I-banks that had offsetting swaps needed the money from the AIG bailout or they would have been caught. Its an (wow powerleveling) insiders game and it takes just a little bit too much time for most people to think (wow gold) through where the AIG 100 billion bailout money went to, hedge funds and players, many of whom hire from the top ranks of DOJ, Fed, Treasury, etc. ZHANG XIAO CHEN

April 19, 2009 10:00 PM  
Blogger marry 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 marry said...

this seems like it would be a very interesting blog to keep up with. Please come visit my site syracuse New York business directory when you got time.

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

Wholesale shoes direct from the factory. Wholesale shoes,, Nike shoes, nike af1, nike shox, nike r4, nike rt1, ATO, BAPE, Bathing Ape, Puma Selling Dunk

July 23, 2009 9:11 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  
Blogger poll 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  
Blogger poll 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  
Blogger 伊凡姐姐 said...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服店

酒店兼差

酒店上班

酒店PT

酒店

August 30, 2009 1:49 AM  
Blogger cheap wow gold said...

my wow gold cheapest wow power leveling cheap wowgold

September 06, 2009 7:30 PM  
Blogger ass said...

www.crazypurchase.com Web site for the majority of the china wholesalers, and buy products wholesale to provide information and buy wholesale from china information exchange platform dedicated to becoming China Wholesale.

September 06, 2009 11:01 PM  
Blogger ass said...

James Dean wore in the blockbuster film Levi jeans and Lacoste polo shirts,Levis Jeans from a "personality and rebellion" symbol. While their counterparts in another company to do jeans Diesel, it has more than the Diesel Jeans another culture, fashion spokesperson. In a series of autumn and winter are filled with a thick knighthood, while below it a cheap jacke is the best proof of this. puma basket t with a nike max shoes make you full of self-confidence. ed hardy shirts make you feel the freedom of street culture with nature.

September 06, 2009 11:02 PM  
Blogger ass said...

In response to multi-era, Burberry will also be designed to reach extends to other areas, such as men's burberry polo shirt, ladies burbrery polos and babolat aeropro drive. A period of change is not just for the Burberry company is celebrating challenges, and it also gives the same with a change in the company. Puma basket and ed hardy clothes is also a product of a new era, the younger generation on the Cheap puma shoes worship has reached frenzied proportions. Wholesale Handbagsand cheap tennis racquets have become a way of reducing consumption cost.

September 06, 2009 11:02 PM  
Blogger Kayla said...

Present-day society, the rapid development of science and technology makes cell phones wholesale is no longer a simple phone communications equipment only. Wholesale cell phones have many features, entertainment, scalability, and so have a good expression of the discount cell phones will be to maximize the effectiveness of multimedia technology.

September 24, 2009 9:31 PM  
Blogger Kayla said...

Cell phones from simple communication became a full-featured entertainment terminal . Can be said that cell phone china new milestone in the industry. This fully demonstrates that, cheap cell phones entertainment has become the most sought after consumer applications and has become the focus of the mobil phones market.

September 24, 2009 9:31 PM  
Blogger poll said...

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

October 02, 2009 7:06 AM  
Blogger poll 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  
Blogger rr8004 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  
Blogger rr8004 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  
Blogger rr8004 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  
Blogger rr8004 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 Adi said...

Oes Tsetnoc | Mengembalikan Jati Diri Bangsa | Kenali dan Kunjungi Objek Wisata di Pandeglang | Oes tsetnoc | Online Marketing | Electronics Gadgets | etips solution | Travel Guide

October 27, 2009 12:42 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  
Blogger rr8004 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  
Blogger rr8004 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  
Blogger Daniela said...

Find your great Travel News and sing the songs at Free Song Lyric or you can watch the drama at Korea Drama Online one of great korea drama is A Love to Kill if you go to travel to Indonesia learn Learn Indonesia Language first! And find your home cari rumah or make a blog Belajar membuat Blog find your home again rumah dijual and again at jual rumah then if you want buy a new laptop see the Laptop Price List or you can buy a New Blackberry and then take care your Health & Jewerly.

November 14, 2009 7:52 PM  
Blogger 後來 said...

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

December 03, 2009 4:49 PM  
Blogger nike 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 nike said...

Clothing has always been a thing that has been given a great importance by human beings. It displays the attitude that the people exhibit.
ed hardy clothes
ed hardy shirts
Lots of brands have been existent in the clothing industry and a famous one among them is the Ed hardy clothing brand.
ed hardy jackets
ed hardy hoodies
The brand got its name from the famous American tattoo artist Ed Hardy. He was a very famous tattoo artist and has published many books on tattooing techniques.
ed hardy boots
ed hardy polo shirts
But his tattooing turned into a brand by the efforts of a company called Christian Audiger. This company was a very famous and very powerful company in the field of clothing.
ed hardy shoes
ed hardy jeans
They felt that it would be appropriate to create a brand called Ed hardy and use Hardy's art as the main selling point for the brand. This venture had turned out to be a very successful one and Ed hardy clothing is one of the most famous brands in the clothing industry.
ed hardy outerwear
ed hardy long sleeve shirts
The brand became very famous because it was worn by many famous celebrities like Madonna, Britney spears and also Sylvester Stallone.
ed hardy bags
ed hardy winter boots
The brand has clothes for men, women, and kids. They have also diversified their business by having a lot of accessories to support their clothing business.
ed hardy love kills slowly shirts
ed hardy love kills slowly shoes
ed hardy love kills slowly boots
for men they have a variety of products such as active wear, denim, outwear, swim trucks, sweaters, t-shirts, tops etc.
ed hardy trousers
ed hardy mens
the accessories include things such as belts, caps, scarves, shoes, socks, jeweler, sunglasses, ties and even wallets.
The art works is a unique combination of American and Japanese cultures. The unique selling proposition of the brand is the way in which the company has used the art works of Ed hardy.
ed hardy womens
ed hardy t shirts
ed hardy sunglasses

December 08, 2009 6:54 PM  
Blogger andragamann said...

keep sharing with us, please....I will waiting your up date everyday!! Have a nice and relax day!..................................................

December 16, 2009 11:02 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 California CA Phone Directory when you got time.

January 06, 2010 2:06 AM  
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 jyoti 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 jyoti 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  
Blogger 娃娃 said...

Gold will not buy everything...................................................

January 18, 2010 11:03 PM  
Blogger more said...

我從來不認為不同意我的看法就是冒犯..............................

February 09, 2010 12:30 AM  
Blogger 佳慧 said...

男同志色教館av影片杜雷斯正妹牆月宮貼圖微風寫真館正妹牆帶回家正妹牆成人網正妹貼圖區正妹牆成人網sexav999免費影片日本情色小說日本情色影片日本情人趣味日本情侶自拍日本情侶成人影片日本愛情動畫日本愛情卡通日本情色bt日本情色av女優日本性教育影片視訊 辣妹美女 視訊線上a片打手槍凹凸電影院愛愛一葉情貼影色站露點girl5320免費a片下載成人情色18成人免費girl5320貼影片foxy下載kiss168成人辣妹視訊論壇男人的最愛線上看av080聊天室 曼雪兒情色文學小說曼雪兒情色文學網曼雪兒免費成人小說曼雪兒小說曼雪兒影片

April 01, 2010 1:07 AM  
Blogger 李惠玲 said...

Many a little makes a mickle.......................................................

April 19, 2010 2:16 AM  

Post a Comment

Links to this post:

Create a Link

<< Home