åãã©ã¡ã¼ã¿ã®ç¯å²æå®
Scalaã®åãã©ã¡ã¼ã¿ã§ã¯ããã©ã¡ã¼ã¿ã§åãä»ããã¯ã©ã¹ã®ç¯å²ãå¶éãããã¨ãã§ãã¾ããJavaã®ãsuperãã¨ããextendsãã¿ãããªæ©è½ã§ãã
- ã[<åãã©ã¡ã¼ã¿å> <: <å>]ãã¨ãããã¨ã§ãã<åãã©ã¡ã¼ã¿å>ã¯<å>æ´¾çã®ä½ããã«å¶éã§ãã¾ãã
- ã[<åãã©ã¡ã¼ã¿å> >: <å>]ãã¨ãããã¨ã§ãã<åãã©ã¡ã¼ã¿å>ã¯<å>ã¾ãã¯ãã®ä¸ä½ã¯ã©ã¹ã®ä½ããã«å¶éã§ãã¾ãã
// ãã¹ãç¨ã¯ã©ã¹ trait A class AA extends A class B // Aã¾ãã¯ãã®ä¸ä½ã¯ã©ã¹ãåãã©ã¡ã¼ã¿ã¨ãã¦åãåãã¯ã©ã¹ class Foo[a <: A] {} //Aã¾ãã¯ãã®ä¸ä½ã¯ã©ã¹ãåãã©ã¡ã¼ã¿ã¨ãã¦åãåãã¯ã©ã¹ class Var[a >: A] {} // ã¯ã©ã¹ã使ããµã³ãã« object Sample { def main(args: Array[String]) { new Foo[A] // OK new Foo[AA] // OK new Foo[B] // ããã¯ã¨ã©ã¼ new Foo[Object] // ããã¯ã¨ã©ã¼ new Var[A] // OK new Var[AA] // ããã¯ã¨ã©ã¼ new Var[B] // ããã¯ã¨ã©ã¼ new Var[Object] // OK } }
ã³ã³ãã¤ã«ããã¨ã以ä¸ã®éãã¨ã©ã¼ã«ãªãã¾ãã
$ scalac GenericsRangeSample.scala GenericsRangeSample.scala:21: error: type arguments [B] do not conform to class Foo's type parameter bounds [a <: A] new Foo[B] // ããã¯ã¨ã©ã¼ ^ GenericsRangeSample.scala:22: error: type arguments [java.lang.Object] do not conform to class Foo's type parameter bounds [a <: A] new Foo[Object] // ããã¯ã¨ã©ã¼ ^ GenericsRangeSample.scala:25: error: type arguments [AA] do not conform to class Var's type parameter bounds [a >: A] new Var[AA] // ããã¯ã¨ã©ã¼ ^ GenericsRangeSample.scala:26: error: type arguments [B] do not conform to class Var's type parameter bounds [a >: A] new Var[B] // ããã¯ã¨ã©ã¼ ^ four errors found
ã»ãã«ããã+ãã-ãã¨ãã<%ãã¨ãã使ããããããã©ãã¾ãä»åº¦!