阅读http://eed3si9n.com/learning-scalaz/Tagged+type.html并尝试示例代码:
import scalaz._; import Scalaz._
sealed trait KiloGram
def KiloGram[A](a: A): A @@ KiloGram = Tag[A, KiloGram](a)
val mass = KiloGram(20.0)
2 * mass
根据指南,应该 yield40.0
但是,在 Scala 2.11.2 上我得到:
scala> 2 * mass
<console>:17: error: overloaded method value * with alternatives:
(x: Double)Double <and>
(x: Float)Float <and>
(x: Long)Long <and>
(x: Int)Int <and>
(x: Char)Int <and>
(x: Short)Int <and>
(x: Byte)Int
cannot be applied to (scalaz.@@[Double,KiloGram])
2 * mass
^
然而
2 * mass.asInstanceOf[Double]
工作得很好。
这是 2.10 vs 2.11 的事情还是我错过了什么?如果我不能像这样(不再)使用它们并且不得不求助于显式强制转换,那么取消装箱的标记类型有什么意义呢?