1
我尝试使用scalaz中的标记类型来加强类型安全性。标签类型:类型不匹配
我遇到了一个警告和一个我不明白的错误。
你能解释我两个吗?
这里是控制台输出:
scala> sealed trait PostId
defined trait PostId
scala> def PostId(l: Long) : Long @@ PostId = Tag[Long, PostId](l)
PostId: (l: Long)[email protected]@[Long,PostId]
warning: previously defined trait PostId is not a companion to method PostId.
Companions must be defined together; you may wish to use :paste mode for this.
scala> case class Post(id: PostId)
defined class Post
scala> Post(PostId(2l))
<console>:26: error: type mismatch;
found : [email protected]@[Long,PostId]
(which expands to) Long with AnyRef{type Tag = PostId}
required: PostId
Post(PostId(2l))
我的不好,应该是'Long @@ PostIdTag',而不是'Long @@ PostId'。我更新了我的答案。 –
一个新的错误:scala> case class Post(id:PostId) error:type mismatch; 找到的:任何 必需:AnyRef 注意:任何不会隐式转换为AnyRef。您可以安全地使用 模式匹配'x:AnyRef'或强制执行'x.asInstanceOf [AnyRef]'来执行此操作。 –