Scalaz未装箱标记类型不会自动取消装箱

前端之家收集整理的这篇文章主要介绍了Scalaz未装箱标记类型不会自动取消装箱前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_2@
阅读 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

根据指南,应该产生40.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还是我错过了什么?如果我不能像这样(不再)使用它们并且必须求助于显式转换,那么未装箱的标记类型有什么意义呢?

解决方法

好的,事实证明这在Scalaz 7.1中被 https://github.com/scalaz/scalaz/pull/693更改了.

基本上标记类型的旧实现被证明是不够安全的,所以它是这样做的,以便在使用“tag”的内容之前必须显式地解包标记类型:

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

感谢S11001001,ceedubs,tpolecat和adelbertC在#scalaz上指出了这一点.

@H_502_2@

猜你在找的Scala相关文章