Scala 类型与类型投影不匹配
我有一个编译器类型不匹配错误,我不明白 给出了Scala 类型与类型投影不匹配,scala,path-dependent-type,type-projection,Scala,Path Dependent Type,Type Projection,我有一个编译器类型不匹配错误,我不明白 给出了Elem和工厂(Companion)的以下定义: 我应该能够做到以下几点: // process elements with peer `Expr[~, A]` trait Impl[S, A, E[~] <: Elem[~] { type Peer = Expr[~, A] }] { implicit def companion: Elem.Companion[E] def test(obj: Obj[S]): Unit =
Elem
和工厂(Companion
)的以下定义:
我应该能够做到以下几点:
// process elements with peer `Expr[~, A]`
trait Impl[S, A, E[~] <: Elem[~] { type Peer = Expr[~, A] }] {
implicit def companion: Elem.Companion[E]
def test(obj: Obj[S]): Unit =
obj.attr[E]("foo").fold(()) { ex =>
val newElem = companion[S](ex)
}
}
trait Elem {
type Peer
}
trait Impl[E[~] <: Elem {type Peer = ~}] {
def foo[R](peer: E[R]#Peer)
foo[Int](??? : E[Int]#Peer)
}
//使用对等'Expr[~,A]处理元素`
性状Impl[S,A,E[~]
val newElem=同伴[S](ex)
}
}
最后一位失败,并显示错误消息:
:62:错误:类型不匹配;
发现:E[S]#对等
(扩展至)Expr[S,A]
必填项:E[S]#对等
(扩展至)Expr[S,A]
val newElem=同伴[S](ex)
^
我将您的示例简化为以下内容:
// process elements with peer `Expr[~, A]`
trait Impl[S, A, E[~] <: Elem[~] { type Peer = Expr[~, A] }] {
implicit def companion: Elem.Companion[E]
def test(obj: Obj[S]): Unit =
obj.attr[E]("foo").fold(()) { ex =>
val newElem = companion[S](ex)
}
}
trait Elem {
type Peer
}
trait Impl[E[~] <: Elem {type Peer = ~}] {
def foo[R](peer: E[R]#Peer)
foo[Int](??? : E[Int]#Peer)
}
我不确定,但这看起来像是一个Scala bug,因为
R
甚至不在错误所在地的范围内——它似乎已经泄漏出去了。Scala似乎无法在细化中执行替换R=Int
,从而允许参数R
保留下来。在自己的项目中已经看到了相同的错误,但不可原谅我不记得它是如何修复的。所以我们将看看人们是否同意这是一个bug。
Error:(12, 18) type mismatch;
found : this.scala.Peer
(which expands to) Int
required: this.Peer(in method foo)
(which expands to) R
foo[Int](??? : E[Int]#Peer)
^^