â»ä¸è¨URLã®ãµã¤ããåèã«ãã¾ãããè±èªãã¾ãã¯æè¡çãªç¥èãè³ãããå
容ã«èª¤ããå«ã¾ããããããããã¾ãã®ã§ããäºæ¿ãã ããã
â»æ¬ã¨ã³ããªã¯åèãµã¤ãã®ç¿»è¨³ããã¼ã¹ã«ãå çã»å¤æ´ãæ½ããæ§æã¨ãªã£ã¦ãã¾ãã
åèãµã¤ã http://confluence.jetbrains.net/display/Kotlin/Generics
å¾ç·¨ Kotlinの総称型(ジェネリクス) 後編 - 算譜王におれはなる!!!!
ç·ç§°åã¯ã©ã¹
Javaã®ããã«Kotlinã®ã¯ã©ã¹ã¯åãã©ã¡ã¼ã¿ãæã¤ãã¨ãã§ãã¾ããä¸è¬ã«ããã®ãããªã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãçæããã«ã¯ãåãã©ã¡ã¼ã¿ãæä¾ããå¿ è¦ãããã¾ããããããã³ã³ã¹ãã©ã¯ã¿ã®å¼æ°ããã®ä»ã®æ段ã«ãããã©ã¡ã¼ã¿ãæ¨è«ã§ããå ´åã¯ãåãã©ã¡ã¼ã¿ãçç¥ãããã¨ãã§ãã¾ãã
class Foo<T>(val t : T) fun main(args : Array<String>) { val foo1 : Foo<String> = Foo<String>("abc") val foo2 = Foo("ABC") }
ç·ç§°åã¯å®è¡æã«ä¿æããã
Javaã¨ã¯ç°ãªããã¯ã©ã¹ã®å®éã®åå¼æ°ã«ã¤ãã¦ã®æ å ±ã¯å®è¡æã«ä¿æããã¾ããããã«ããå®è¡æã«ãªãã¸ã§ã¯ãã®å®å ¨ãªåããã§ãã¯ãããã¨ãå¯è½ã«ãªãã¾ãã
if(hoge is Hoge<Int>) { // 'is'ã¯Javaã§è¨ã'instanceof' // hogeã¯åãªãHogeã§ã¯ãªããHoge<Int>ã§ãããã¨ãä¿è¨¼ããã¦ãã }
// Java //if(hoge instanceof Hoge<Int>) { // ãã®å½¢ã¯Javaã§ã¯ã³ã³ãã¤ã«ã¨ã©ã¼ã¨ãªã if(hoge instanceof Hoge) { // ç²¾ã ããã®å½¢ }
isãã§ãã¯ã«ã¯ãåå¤æ°ã使ç¨ã§ãã¾ãã
if (foo is T) { // this is a non-trivial check // ... }http://confluence.jetbrains.net/display/Kotlin/Generics ããå¼ç¨
Kotlinã«ã¯rawåï¼æªå å·¥åï¼ã¨åæ¶å»ï¼type erasureï¼ã¯ããã¾ãããã¤ã¾ããç·ç§°åã¯ã©ã¹ã¯åãã©ã¡ã¼ã¿ãªãã§ã¯åå¨ã§ãã¾ãããããã¯Effective Javaã®é ç®23ãDon't use raw types in new code*1ãã¨é ç®24ãEliminate unchecked warnings*2ãã«å¯¾å¿ãã¦ãã¾ãã
åæ£
Javaã®åã·ã¹ãã ã®ä¸ã§æãããªããã¼ãªãã¼ãã®ã²ã¨ã¤ã¯ãã¯ã¤ã«ãã«ã¼ãå*3ã§ãããKotlinã«ã¯ããã¾ããã
ã¯ããã«Javaã§ã¯ä½æ ããã¹ããªã¢ã¹ãªã¯ã¤ã«ãã«ã¼ããå¿ è¦ãªã®ãèãã¾ãããã
ã¨ãåèãµã¤ãã«ã¯ããã¾ãããé·ããªãã®ã§å²æâ¦ï¼¾ï¼¾ï¼
詳ããã¯ãEffective Javaã®é
ç®28ãUse bounded wildcards to increase API flexibility*4ããåç
§ãã¦ãã ãããã¾ãã項目28 APIの柔軟性向上のために境界ワイルドカードを使用する - プログラマ的京都生活ã§ã¯é常ã«ãããããã解説ãããã¦ãã¾ãã®ã§ãä½µãã¦ã覧ãã ããã
â»マイクロソフトのC#を解説するページã«å£ã£ã¦ãããã§ã¯varianceã¨ããè¨èããåæ£ãã¨ç¿»è¨³ãã¦ãã¾ãã
宣è¨ç®æåæ£
è¦ããã«Javaã®ç·ç§°åã¯ä¸å¤ï¼invarianceï¼ã§ãããã¨ããããã§ã¯åé¡ã¨ãªã£ã¦ãã¾ããããã«å¯¾ãKotlinã§ã¯C#ã®ãããªä»çµã¿ãç¨æããã¦ãã¾ããã¤ã¾ãåãã©ã¡ã¼ã¿ã«å ±å¤æ§ï¼covarianceï¼ã¾ãã¯åå¤æ§ï¼contravarianceï¼ã¨è¨ã£ãæ§è³ªãæããããã¨ãã§ãã¾ãã
å ±å¤æ§ãæå®ããã«ã¯ãåãã©ã¡ã¼ã¿ã«out修飾åãä»ãã¾ãã
abstract class Source<out T> { fun nextT() : T } fun demo(strs : Source<String>) { val objects : Source<Any> = strs // This is OK, since T is an out-parameter // ... }http://confluence.jetbrains.net/display/Kotlin/Generics ããå¼ç¨
out修飾åã¯åæ£ã¢ããã¼ã·ã§ã³ï¼variance annotationï¼ã¨è¨ããããã¯åãã©ã¡ã¼ã¿ã®å®£è¨ç®æã§æä¾ããã¦ããã®ã§ããã®æ©è½ã宣è¨ç®æåæ£ã¨å¼ãã§ãã¾ãã
ããã¦åãã©ã¡ã¼ã¿ã«åå¤æ§ãæå®ããinã¢ããã¼ã·ã§ã³ãããã¾ãã
abstract class Comparable<in T> { fun compareTo(other : T) : Int } fun demo(x : Comparable<Number>) { x.compareTo(1.0) // 1.0 has type Double, which is a subtype of Number // Thus, we can assign x to a variable of type Comparable<Double> val y : Comparable<Double> = x // OK! }http://confluence.jetbrains.net/display/Kotlin/Generics ããå¼ç¨
使ç¨ç®æåæ£ï¼åæå½±
宣è¨ç®æåæ£ã§ã¯ãåæ£ã¢ããã¼ã·ã§ã³ãã¯ã©ã¹å®£è¨ã®ã¨ãã«ä½¿ç¨ãã¾ãããã使ç¨ç®æåæ£ï¼use-site varianceï¼ã§ã¯ãåæ£ã¢ããã¼ã·ã§ã³ããªãã¸ã§ã¯ãã®ä½¿ç¨ç®æã§å®£è¨ãã¾ããã¤ã¾ããã¡ã½ããçã®ä»®å¼æ°ã®åã« out ã in ãä»å ãã¦ãåã®ç¡¬ç´æ§ã解æ¶ãã¾ããããã¦ããã®ä½¿ç¨ç®æåæ£ã®ãã¨ãåæå½±ï¼type projectionï¼ã¨å¼ã³ã¾ãã
ãã£ããããããªèª¬æã§ãã¿ã¾ããã詳細ã¯Generics - Kotlin - Confluenceãã覧ãã ããã
ã¹ã¿ã¼ã»ããã¸ã§ã¯ã·ã§ã³
åãã©ã¡ã¼ã¿ã«ã¤ãã¦ã¯ä½ãç¥ããªããã©å®å
¨ãªæ¹æ³ã§ä½¿ããããã¨è¨ããããªãã§ããããããã§ã®å®å
¨ãªæ¹æ³ã¨ã¯ãoutããã¸ã§ã¯ã·ã§ã³ãæ±ã£ããããã®ããã¸ã§ã¯ã·ã§ã³ãã対å¿ãããã©ã¡ã¼ã¿ã®ä¸éã§ãããã¨ï¼ã¤ã¾ããã»ã¨ãã©ã®ã±ã¼ã¹ã§ã¯out Any?ï¼ãè¨ãã¾ããKotlinã§ã¯ã¹ã¿ã¼ã»ããã¸ã§ã¯ã·ã§ã³ã¨å¼ã°ããéè¨æ§æãæä¾ããã¦ãã¾ããFoo<*>ã¯ãFoo
注æï¼ã¹ã¿ã¼ã»ããã¸ã§ã¯ã·ã§ã³ã¯Javaã®rawåã«é常ã«ä¼¼ã¦ãã¾ãããå®å ¨ã§ãã
Kotlinã®ã¸ã§ããªã¯ã¹ã®ã話ã®ç¶ãã¯ã¾ãä»åº¦ãï¼
å¾ç·¨âKotlinの総称型(ジェネリクス) 後編 - 算譜王におれはなる!!!!