Skip to content

Instantly share code, notes, and snippets.

@f81
Created March 31, 2014 08:10
Show Gist options
  • Save f81/9887549 to your computer and use it in GitHub Desktop.
Save f81/9887549 to your computer and use it in GitHub Desktop.
第21章:Scalaの型パラメータ ref: http://qiita.com/f81@github/items/7a664df8f4b87d86e5d8
public class ArrayList<E> {...}
final List<String> arrays = new ArrayList<String>()
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]
^
scala> class School[A >: Person]
defined class School
scala> new School[Animal]
res0: School[Animal] = School@4d832421
scala> new School[Person]
res1: School[Person] = School@4d032cfc
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]
^
scala> class School[A >: Student <: Animal]
defined class School
final List<String> list = new ArrayList<String>();
final List<Object> objList = list;
sealed abstract class List[+A] extends AbstractSeq[A] with LinearSeq[A] with Product with GenericTraversableTemplate[A, List] with LinearSeqOptimized[A, List[A]]
class List[+A]
scala> class School[+T]
defined class School
scala> def receive(args: School[Person]) = println("success!!!")
receive: (args: School[Person])Unit
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)
}
}
scala> receive(new School[Person])
success!!!
scala> receive(new School[Student])
success!!!
scala> receive(new School[Animal])
<console>:12: error: type mismatch;
found : School[Animal]
required: School[Person]
receive(new School[Animal])
^
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])
^
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])
^
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)
}
$ scalac TypeParamSample.scala
$ scala TypeParamSample
test
1
scala> class Animal
defined class Animal
scala> class Person extends Animal
defined class Person
scala> class Student extends Person
defined class Student
scala> class School[A <: Person]
defined class School
scala> new School[Student]
res0: School[Student] = School@7de3ec13
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