Monday, February 22, 2010

Are Generic Methods evil?

Over the weekend I ran into an interesting design issue inside of ERRest. Java supports declaring generic methods that can perform a simple type inference:

public class NonGenericClass {
    public  T objectForKey(String key) { ... }
}

What this allows you to do is:

Person p = new NonGenericClass().objectForKey("person")

Notice that we don't have to cast to Person like you would if objectForKey returned Object. So ... is this bad form? The closest examples of Sun using type inference in the core libraries is Collections.emptyList(), which can give you a type-inferred List. The difference, though, is that this is an inherently safe operation. In the example above, that code is inherently unsafe. On the upside, your API becomes easier to use -- your users don't need to think about the cast. On the downside, you might say the API is misleading, implying that this operation is in some way typesafe when it clearly is not.

I came to a happy place with it. My decision was that if this API is impossible to make type-safe (think ResultSet.getObject(String)), and if that's fairly obvious to the user of the API, then taking advantage of type inference is OK and will just save your API users time.

One catch, by the way, is that javac appears to not like a double indirection of type inference:

public class ClassOne {
    public  T methodOne() {
        return ...;
    }
}

public class ClassTwo {
    public  T methodTwo() {
      return new ClassOne().methodOne(); // this is a compile error in javac
    }
}

For some reason, javac is not capable of returning an inferred type for a method call to a method that returns and inferred type. You have to cast to T:

public class ClassTwo {
    public  T methodTwo() {
      return (T)new ClassOne().methodOne(); // this makes javac happy
    }
}

Incidentally, the Eclipse compiler is fine with the first one and doesn't need the cast. I think this is a javac bug, personally.

39 comments:

傻眼 said...
This comment has been removed by a blog administrator.
士凱 said...

以簡單的行為愉悅他人的心靈,勝過千人低頭禱告。........................................

彭志文 said...

有用的才華若不用,便如同日晷儀放在陰暗之中......................................................

KarieS美航嘉雯 said...

t 台灣藝術台辣妹有約 辣妹情色視訊 辣妹性感 辣妹影音聊天網 聊天室環球辣妹34c聊天室 辣妹亞洲成人圖片區 辣妹有約溫泉 辣妹性愛文學 辣妹哈啦聊天室 辣妹貼圖a片天堂 辣妹自拍偷拍 辣妹聊天室 90691比基尼辣妹影片 超性感辣妹 視訊情人辣妹080 視訊辣妹援交女 視訊辣妹援交 視訊辣妹自慰 視訊辣妹34c甜心寶貝直播貼片 視訊辣妹080 視訊辣妹,成人,嘉義視訊 aio性感辣妹34c甜心寶貝貼片 aio辣妹交友愛情館 aaio辣妹視訊 比基尼av辣妹影片 辣妹聊天室90691 辣妹影音 辣妹貼圖站 本土美少女辣妹貼圖 辣妹有約綜藝節目 辣妹貼圖站 辣妹情色網 情色視訊交友辣妹no3 性感辣妹交友 脫衣亞洲辣妹 玩美辣妹有約 環球辣妹聊天室 視訊辣妹34c 辣妹妹影音視訊聊天室 兼職援交友辣妹視訊 高雄辣妹視訊 桃園辣妹視訊 台北辣妹視訊 台中辣妹視訊 名模辣妹聊天室 885cc 85cc成人片試看 85cc女 85cc免費影城85cc免費影城 85cc免費影片觀賞 85cc免費影片短片 85cc自拍 85cc無碼影 85cc 亞洲短片 a交友愛情館影片免費85cc

裕以 said...

Make hay while the sun shines.......................................................

406IraidaMathew0 said...

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

蘇pet0701em_halvorsen said...

TAHNKS FOR YOUR SHARING~~~VERY NICE ........................................

淑娟 said...

hello~welcome my world~<. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Bob0104byWas0104ham said...

動不一定每一次都成功,但坐而不行,絕無任何成功可言.............................................

517SeritaG_Jamar0 said...

成人影視 台灣論壇女生 彩虹sexmovie影城 八國聯軍成人成人短片 台南視訊,080情人網 免費試看av,論壇區,色情 網站 下載色情短片 北台灣視訊援交聊天室 視訊聊天室90739 黑澀會 視訊 性福免費影片分享 成人交友mmshow avhigh視訊聊天 5i0美色女影城 ut影音視訊聊天室13077 情色網站live 南台灣美女視訊 性愛貼圖區 成人聊天室ut ut網際空間聊天 kk視訊,色情 a 片 一夜情 免費成人 免費視訊聊天 msn 免費成年人影片 777成人區 亞洲藝人辣妹自拍 85cc成人片觀看 完美情人視訊聊天室 新 avhigh視訊交友 性愛俱樂部 一夜情sex999免費影片 免費a片卡通 捷克論壇 s383情色大網咖情人視訊網 111av影片 美眉 線上LIVE 美女視訊聊天網 微風成人免費成人遊戲 免費影音hibb 5278cc免費影片 高雄視訊辣妹自拍 免費聊天 d760 免費e爵娛樂 a片免費試看avdvd 色漫畫帝國sex888免費看影 台南視訊,免費18遊戲 plusz色女人專用 色妹妹天堂免費情慾影片觀賞 919sex色片直播網 免費 aa 片試看,成人影片分享 情色論壇sex貼片

柏勳 said...

you‘re so smart!............................................................

桂竹桂竹 said...

感覺很好的blog,祝你開心喔........................................

寶堅強皓 said...
This comment has been removed by a blog administrator.
AlysiaDraeger0417永瑞 said...
This comment has been removed by a blog administrator.
reeselane said...
This comment has been removed by a blog administrator.
禎峰 said...
This comment has been removed by a blog administrator.
家弘 said...
This comment has been removed by a blog administrator.
EulaliaS_Bro仁豪 said...
This comment has been removed by a blog administrator.
于庭 said...
This comment has been removed by a blog administrator.
雲亨雲亨雲亨 said...
This comment has been removed by a blog administrator.
育隆 said...

海鷗要高飛,必先遠退。花蜜要香醇,必先久釀。............................................................

JasonBirk佳琪 said...

留言支持好作品~加油!期待進步和更新..................................................................

家愛家愛 said...

人生之中,比冒險更危險的一件事:不去冒險。..................................................

楊儀卉 said...

教育的目的,不在應該思考什麼,而是教吾人怎樣思考............................................................

王美妹 said...

這不過是滑一跤,並不是死掉而爬不起來了。..................................................

黃威宇 said...

Man proposes, God disposes.

佳瑩佳瑩 said...

愛情是一種發明,需要不斷改良。只是,這種發明和其他發明不一樣,它沒有專利權,隨時會被人搶走。.................................................................

吳婷婷 said...

愛,拆開來是心和受兩個字。用心去接受對方的一切,用心去愛對方的所有。......................................................................

翔馨陳陳翔馨陳陳 said...

文章不求沽名釣譽,率性就是真的..................................................................

怡于名君 said...

到處盡心,即為快事;舉步踏實,便是坦途。......................................................

建邱勳 said...

臨淵羨魚,不如退而結網。......................................................

建邱勳 said...

所有的資產,在不被諒解時,都成了負債..................................................................

怡涂涂涂均 said...

人生像一杯茶,若一飲而盡,會提早見到杯底..................................................

幸平平平平杰 said...

財富並非永遠的朋友,但朋友卻是永遠的財富。......................................................................

江趙雲虹趙雲虹仁昆 said...

拒絕冒險和成長的人,終將被生命的潮流陶汰。..................................................

至馬馬馬馬玄馬馬馬馬 said...

相見亦無事,不來常思君......................................................................

翊翊翊翊張瑜翊翊翊 said...

來打聲招呼-大家好!!!............................................................

黃英吳思潔吳思潔邦 said...

精彩的部落格 值得一推再推 支持你

孟湖聿軒 said...

很不賴的分享!! 多謝啦!!◑0◐......................................................