ãå®å ¨ãããªããæ©è½ãç°¡åã«ä½¿ããããã«ãªã£ã¦ããã®ã¯ãã©ã¡ããã¨ããã¨JVMã®ä¸ã§Scalaãå®ç¨è¨èªã¨ãã¦ãã£ã¦ããããã®å¿ è¦æªã¿ãããªãã®ã§ãã£ã¦ãScalaãè¨èªã¨ãã¦å®å ¨æ§ãéè¦ãã¦ããªãã¨ãããå æ¸ã«æ±ã£ã¦ãããã¨ããããã§ã¯ç¡ãã®ã ã¨çè§£ãã¦ãã¾ã
Re: Re: Re: 不満の記録
ãã³ã: JVM ãæãæ¨ã¦ã
ã¨ããã®ã¯åè«ã§ãããå¤é¨ã©ã¤ãã©ãªãå¼ã³ããããªã©ã¨ããä¸ä¿çãªçç±ã§åå®å
¨æ§ãæ¨ã¦ãã®ã¯ãã£ãããªããªã¼ãã¨æããã§ãã
Scala ã«ã¨ã£ã¦ã® Java ã¨ããã®ã¯ Ruby/OCaml/Haskell ã«ã¨ã£ã¦ã® C ãªããã§ãJava ã©ã¤ãã©ãªãå¼ã¶ããã«ã¯æç¤ºçãªãã£ã¹ãç¨åº¦ã®æ³¥èãã¯ãã£ã¦ãããã®ã«ãJava ã¢ãã¬ãããªããä½ã£ãã®ã§ããã¡ããã¡ãæ©æµã«ãããã£ã¦ãããã§ããã
ããããæãéçãªæ©è½ãã¦ã¼ã¶ããã©ã®ç¨åº¦å®¹æã«ä½¿ããããã«ãããã¯ãè¨èªèªä½ã®åå®å ¨æ§(ãããã¯å ç¢æ§)ã¨ã¯ããã¾ãé¢ä¿ãªã話ã§ã¯ãªããã¨ããã¨ãã°ãOCamlã®Obj.magicã ã£ã¦é»éè¡æ±ãããã¦ã¯ãããã®ã®ãã¦ã¼ã¶ããæ®éã«ä½¿ããªããã¨ããã¨ãããããã¨ã¯ç¡ãããã§ãã£ã¦ããããOCamlã¯å ç¢ã§ã¯ãªãã¨ãåå®å ¨ã§ã¯ãªãã¨ãã¨ããã¨ãããããªãã ããã¨æãããã§ãã
ãã¿ã¼ã³ãããã¿ãããªè¨èªã®ã³ã¢ã«é»éè¡ãæ®ã£ã¦ãã®ãæ°ã«ãã¦ã¾ãã
Any#asInstanceOf 㯠(çµã¿è¾¼ã¿ã§ã¯ãããã©) ãã ã®ã¡ã½ãããªã®ã§ããªãã許容ç¯å²ã§ããmagic ã¨ã unsafePerformIO ã¿ããã«ãã£ã¨é»éè¡ã£ã½ãååã«ããã¨ãªãããã¨æãã¾ãããasInstanceOf ã§ããã°ãããªé¦ãã¯ãããã¾ããã
åã«ããããããã¿ã¼ã³ã®åé¡ã¯ããT[A] ãªåã®ãã¿ã¼ã³ã¯ T[Any] ãã許ããªããã¨ããæãã®å¶ç´ãå ¥ããã ãã§ç°¡åã«è§£æ±ºã§ãã¾ããã (åéãï¼) ããããå¾ã« Any ã asInstanceOf[A] ããå¿ è¦ãããã¾ããããã®ç¨åº¦ã®å©ä¾¿æ§ã¨åå®å ¨æ§ã¨ã®ãã¬ã¼ããªããªããåå®å ¨æ§ãé¸ã¹ã°ããã®ã«ãªããã¨æãã®ã§ããã
追è¨
ããåéãã ã
Any ã§ããã®ã¯ co-variant ãªå ´åã ããcontra-variant ãªã Nothing ããªãnon-variant ãªãéçã«åãã§ãã¯ã§ãããã ããå®éä»ã§ããã£ã¦ãã£ã½ãï¼ existential type ã¨ã structural type ã¨ãã¯ç¥ããªãã
scala> val f = (x:Int) => x + 1 f: (Int) => Int = <function> scala> (f:(Nothing)=>Any)(42) <console>:6: error: type mismatch; found : Int(42) required: Nothing (f:(Nothing)=>Any)(42) ^ scala> f match { case g:((Nothing)=>Any) => g(42) } warning: there were unchecked warnings; re-run with -unchecked for details res1: Int = 43
ããã¼ãããªããããåãã§ãã¯ãéãï¼
scala> f match { case g:((Nothing)=>Any) => g("foo") } <console>:6: error: type mismatch; found : java.lang.String("foo") required: Int f match { case g:((Nothing)=>Any) => g("foo") } ^
ããã¯éããªãã®ãã
scala> List(1) match { case l:List[String] => l.head ++ "foo" } <console>:5: warning: match is not exhaustive! missing combination $colon$colon List(1) match { case l:List[String] => l.head ++ "foo" } ^ warning: there were unchecked warnings; re-run with -unchecked for details java.lang.ClassCastException: java.lang.Integer (snip)
ãã£ã¡ã¯éãã®ããæ··ä¹±ãã¦ããã
scala> f match { case g:((Nothing)=>String) => g } warning: there were unchecked warnings; re-run with -unchecked for details res2: (Int) => Int with (Nothing) => String = <function>
ããããtrait ãªãã¦ãã®ããã£ããªãããã㯠(Nothing)=>Any ã¨ãããã¿ã¼ã³ããæ¸ããªãããã«ããã°åé¡ã¯èµ·ããªãæ°ã¯ãããã©ãã¾ã ããããè¦è½ã¨ãã¦ããã
ããã«è¿½è¨
ããããnon-variant ããããããã¡ã ã
scala> (Array(1, 2, 3):AnyRef) match { case ary:Array[String] => ary(0) = "foo" } scala.MatchError: [I@ac2d3c (snip)
å®ç¨æ§ãè½ã¨ããã«ããããµããã®ã¯ç¡çããã ãã¨ãããã¨ã§ãã£ã±ãã¼ãã®åéãã§ããããã¿ã¾ããã