4

阅读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 的事情还是我错过了什么?如果我不能像这样(不再)使用它们并且不得不求助于显式强制转换,那么取消装箱的标记类型有什么意义呢?

4

1 回答 1

7

好的,原来这在 Scalaz 7.1 中被https://github.com/scalaz/scalaz/pull/693改变了。

基本上,标记类型的旧实现被证明不够安全,因此在使用“标记”的内容之前,需要对标记类型进行显式解包:

scala> trait Kg
scala> val Kg = Tag.of[Kg]
scala> val mass = Kg(15.0)
scala> 3 * Kg.unwrap(mass)
res0: Double = 45.0

感谢#scalaz 上的 S11001001、ceedubs、tpolecat 和 adelbertC 指出这一点。

于 2014-09-30T23:16:47.247 回答