Created
March 31, 2014 08:10
-
-
Save f81/9887549 to your computer and use it in GitHub Desktop.
第21章:Scalaの型パラメータ ref: http://qiita.com/f81@github/items/7a664df8f4b87d86e5d8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayList<E> {...} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final List<String> arrays = new ArrayList<String>() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> new School[Animal] | |
<console>:10: error: type arguments [Animal] do not conform to class School's type parameter bounds [A <: Person] | |
val res2 = | |
^ | |
<console>:11: error: type arguments [Animal] do not conform to class School's type parameter bounds [A <: Person] | |
new School[Animal] | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> class School[A >: Person] | |
defined class School |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> new School[Animal] | |
res0: School[Animal] = School@4d832421 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> new School[Person] | |
res1: School[Person] = School@4d032cfc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> new School[Student] | |
<console>:11: error: type arguments [Student] do not conform to class School's type parameter bounds [A >: Person] | |
val res2 = | |
^ | |
<console>:12: error: type arguments [Student] do not conform to class School's type parameter bounds [A >: Person] | |
new School[Student] | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> class School[A >: Student <: Animal] | |
defined class School |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final List<String> list = new ArrayList<String>(); | |
final List<Object> objList = list; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A, List] with LinearSeqOptimized[A, List[A]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class List[+A] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> class School[+T] | |
defined class School | |
scala> def receive(args: School[Person]) = println("success!!!") | |
receive: (args: School[Person])Unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object TypeParamSample { | |
class TypeParam[T](val t: T) { | |
def get: T = this.t | |
} | |
def main(args: Array[String]): Unit = { | |
val stringTypeParam = new TypeParam[String]("test") | |
println(stringTypeParam.get) | |
val intTypeParam = new TypeParam[Int](1) | |
println(intTypeParam.get) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> receive(new School[Person]) | |
success!!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> receive(new School[Student]) | |
success!!! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> receive(new School[Animal]) | |
<console>:12: error: type mismatch; | |
found : School[Animal] | |
required: School[Person] | |
receive(new School[Animal]) | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> class School[-T] | |
defined class School | |
scala> def receive(args: School[Person]) = println("success!!!") | |
receive: (args: School[Person])Unit | |
scala> receive(new School[Person]) | |
success!!! | |
scala> receive(new School[Animal]) | |
success!!! | |
scala> receive(new School[Student]) | |
<console>:13: error: type mismatch; | |
found : School[Student] | |
required: School[Person] | |
receive(new School[Student]) | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> class School[T] | |
defined class School | |
scala> def receive(args: School[Person]) = println("success!!!") | |
receive: (args: School[Person])Unit | |
scala> receive(new School[Person]) | |
success!!! | |
scala> receive(new School[Animal]) | |
<console>:12: error: type mismatch; | |
found : School[Animal] | |
required: School[Person] | |
Note: Animal >: Person, but class School is invariant in type T. | |
You may wish to define T as -T instead. (SLS 4.5) | |
receive(new School[Animal]) | |
^ | |
scala> receive(new School[Student]) | |
<console>:13: error: type mismatch; | |
found : School[Student] | |
required: School[Person] | |
Note: Student <: Person, but class School is invariant in type T. | |
You may wish to define T as +T instead. (SLS 4.5) | |
receive(new School[Student]) | |
^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TypeParam[T](val t: T) { | |
def get: T = this.t | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(args: Array[String]): Unit = { | |
val stringTypeParam = new TypeParam[String]("test") | |
println(stringTypeParam.get) | |
val intTypeParam = new TypeParam[Int](1) | |
println(intTypeParam.get) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ scalac TypeParamSample.scala | |
$ scala TypeParamSample | |
test | |
1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> class Animal | |
defined class Animal | |
scala> class Person extends Animal | |
defined class Person | |
scala> class Student extends Person | |
defined class Student |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> class School[A <: Person] | |
defined class School |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> new School[Student] | |
res0: School[Student] = School@7de3ec13 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> new School[Person] | |
res1: School[Person] = School@35203079 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment