時々、私は次のようなものが含まれているJavaを扱います。
def printDbl(d:Double) { println("dbl: " + d) }
def printInt(i:Int) { println("int: " + i) }
当然、これをいくつかのscalaでラップしたいのですが、最終的には次のようになります。
def print[T:Manifest] (t:T) {
if (manifest[T] <:< manifest[Int]) { printInt(t.asInstanceOf[Int]) ; return }
if (manifest[T] <:< manifest[Double]) { printDbl(t.asInstanceOf[Double]) ; return }
throw new UnsupportedOperationException("not implemented: " + manifest[T])
}
しかし、以下を実行すると、ランタイム例外が発生します。
print(1)
print(2.0)
print("hello")
コンパイル時にこれをキャッチする方法があったことを思い出しているようですが、グーグルで検索できないようです。おそらくいくつかの巧妙な暗黙の変換?