by shigemk2

当面は技術的なことしか書かない

型パラメータの制約

scala> class X[A] {
     |   def exec(implicit t:A =:= Int):Unit = println("exec")
     | }
defined class X

scala> val x = new X[Int]
x: X[Int] = X@66b60da0

scala> x.exec
exec

scala> val x2 = new X[String]
x2: X[String] = X@153d4e5a

scala> x2.exec
<console>:11: error: Cannot prove that String =:= Int.
              x2.exec
                 ^

だいたいこんな感じ。

A=:=Bみたいな感じで。

上の例だと引数の型がIntじゃないと弾かれる。