1
我使用類型約束來說類型A
和B
必須相同。在下面,我被允許隱含地從A
投到B
,但需要明確演員從B
到A
。什麼是正確的方法來做到這一點?相等的類型約束需要顯式轉換
class Pair[A, B](var first: A, var second: B) {
def swap()(implicit ev: A =:= B) {
val tmp = second
second = first
first = tmp //won't compile without appending .asInstanceOf[A]
}
/*
...other methods like replaceFirst, replaceSecond that
don't require types A, B to be the same
*/
}