Scalaã¨Kotlinï¼ã¨æã®Javaï¼ã®ã¸ã§ããªã¯ã¹ãå£ãã¦ããçç±
表é¡ã®éãã§ããã¨ãããããKotlinçã¨Scalaçã®ã³ã¼ãè²¼ãã¾ãã
ãããã®ã³ã¼ãã§ã¯ã両æ¹ã¨ãæ示çãªãã¦ã³ãã£ã¹ãããã®ä»ã®æãç©´ã使ã£ã¦ããªãã®ã§ãå®è¡ãã¦ã決ãã¦ClassCastException
ãèµ·ãã¦ã¯ãããªãã®ã§ãããå®éã«å®è¡ããã¨ClassCastException
ãèµ·ãã¦ãã¾ãã¾ãã
ãã¦ãããã¯ã³ã³ãã¤ã©ã®å®è£ ã®ãã°ã§ããããè¨èªä»æ§ã®ãã°ã§ãããã®ã§ãããã¡ãã£ã¨çç±ã説æãã¦ã¿ããã¨æãã¾ãã
class B extends A with Comparable[B] { def compareTo(b: B): Int = 0 }
ä¸è¨ã®ã³ã¼ããã³ã³ãã¤ã«ããã¨ãä¸è¨ã®ã³ã¼ããçæããã¾ããããã§ãint compareTo(java.lang.Object)
ã¨ããã¡ã½ããã
å®ç¾©ããã¦ããã®ãå³åã§ãã
Compiled from "C.scala" public class B extends A implements java.lang.Comparable<B> { public int compareTo(B); Code: 0: iconst_0 1: ireturn public int compareTo(java.lang.Object); Code: 0: aload_0 1: aload_1 2: checkcast #2 // class B 5: invokevirtual #19 // Method compareTo:(LB;)I 8: ireturn public B(); Code: 0: aload_0 1: invokespecial #25 // Method A."<init>":()V 4: return }
ãã®ãããªãã¸ã§ããªã¯ã¹ããã¾ãåããããã«ã³ã³ãã¤ã©ãèªåçæããã¡ã½ããã®ãã¨ãããªãã¸ã¡ã½ãã
ã¨å¼ãã ããã¾ããããã®ããªãã¸ã¡ã½ãããæ²è
ã§ãããã®ããªãã¸ã¡ã½ããã§ã¯åã«å¼æ°ãBåã«ãã£ã¹ããã¦å¼ã³åºãã ãã§ãããããã¯Comparable<B>
åã¨ãã¦åãæ±ãããåã«å¯¾ãã¦æ£ããcompareTo
ã¡ã½ãããå¼ã³åºãããã«å¿
è¦ãªãã®ã§ãããã®ããªãã¸ã¡ã½ãããæ¬æ¥ã¯å
é¨å®è£
ã§ãã£ã¦ç´æ¥å¼ã³åºãã¦ãã¾ãã¨ã¾ãã代ç©ãªã®ã§ãããããªãã¸ã¡ã½ããã¨åãã·ã°ããã£ã®ã¡ã½ãããã¹ã¼ãã¼ã¯ã©ã¹ã§å®ç¾©ãã¦ããã¨ããã®ããªãã¸ã¡ã½ãããã¦ã¼ã¶ã¼ãå¼ã³åºãã¦ãã¾ãããã®çµæãClassCastExceptionãèµ·ããã®ã§ããã
å®ã¯ãã®åé¡ãJava Genericsã®ï¼å®è£ and/or ä»æ§ï¼ãã°ã¨ãã¦ä»¥åããç¥ããã¦ããã
Java Generics Unsound? | A Concurrent Affair
çã§è¨åããã¦ãã¾ããçµæãJava Genericsã§ã¯ãã®ãããªããªãã¸ã¡ã½ãããã¦ã¼ã¶ãå¼ã³åºãå¯è½ã«ãªããããªç©´ã¯ãµããããã®ã§ãããã¸ã§ããªã¯ã¹ãJavaã¨é¡ä¼¼ã®æ¹å¼ã§ã³ã³ãã¤ã«ããScalaãKotlinã¯ãã®ä»æ§ãã°ããã®ã¾ã¾å¼ãç¶ãã§ãã¾ã£ãã®ã§ããã
ãªããKotlinã®issue trackerã«ã¯æ¢ã«å ±å
https://youtrack.jetbrains.com/issue/KT-13712
ãã¦ããã¾ãã