[英]How to make scalaz.Tagged work with class class primitive attributes?
Consider the following example: 请考虑以下示例:
import scalaz._
object TaggedExample {
sealed trait Test
def Test[A](a: A): A @@ Test = Tag[A, Test](a)
}
case class TaggedAttribute(l: Long @@ TaggedExample.Test)
It will fail to compile with next reason: 它将无法编译,原因如下:
scalac: type mismatch;
found : Double
required: AnyRef
Note: an implicit exists from scala.Double => java.lang.Double, but
methods inherited from Object are rendered ambiguous. This is to avoid
a blanket implicit which would convert any scala.Double to any AnyRef.
You may wish to use a type ascription: `x: java.lang.Double`.
To my understanding, it is happens due to some details in case class compiler code generation (because simple def test(l: Long @@ TaggedExample.Test) = l
compiles just fine). 根据我的理解,它是由于案例类编译器代码生成中的一些细节而发生的(因为简单的def test(l: Long @@ TaggedExample.Test) = l
编译得很好)。
If we change case class definition to 如果我们将案例类定义更改为
case class TaggedAttribute(l: java.lang.Long @@ TaggedExample.Test)
compilation will succeed. 编译将成功。
The question is: Is there a way to avoid this scalac
error without changing type of l
to java.lang.Long
(which, in turn, will allow l
to be null
etc.)? 问题是:有没有办法避免这个scalac
错误而不改变l
类型为java.lang.Long
(反过来,它将允许l
为null
等)?
Found this Tagged type : type mismatch question and answer to it right after posting, but still: may be there is a way to avoid use of java.lang.*
box types. 找到这个Tagged类型:在发布后立即输入不匹配的问题和答案,但仍然:可能有一种方法可以避免使用java.lang.*
box类型。
Know bug: Compiler error when using tagged types and case classes 了解bug: 使用标记类型和案例类时编译器错误
Workaround : use box types. 解决方法 :使用框类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.