Scala 带有逆变类型参数的继承返回类型
我有一个协变类型Scala 带有逆变类型参数的继承返回类型,scala,generics,inheritance,variance,contravariance,Scala,Generics,Inheritance,Variance,Contravariance,我有一个协变类型a的特征,它声明(除其他外)此方法: MaxPQ[+U]{ ... def insert[K>:U:订购](v:K):此类型 ... } 抽象类AbstractMaxPQ[U:Ordering]扩展了MaxPQ[U] 我还需要使用以下组合: trait FindMin[U]{ self:AbstractMaxPQ[U]=> 我返回this.type,因为我希望基于Node或Array的优先级队列实现返回当前类型,而不是MaxPQ[U] 在基于阵列的实现中,我有以下几点: cl
a
的特征,它声明(除其他外)此方法:
MaxPQ[+U]{
...
def insert[K>:U:订购](v:K):此类型
...
}
抽象类AbstractMaxPQ[U:Ordering]扩展了MaxPQ[U]
我还需要使用以下组合:
trait FindMin[U]{
self:AbstractMaxPQ[U]=>
我返回this.type
,因为我希望基于Node
或Array
的优先级队列实现返回当前类型,而不是MaxPQ[U]
在基于阵列的实现中,我有以下几点:
class ArrayMapQ[U:Ordering:ClassTag]…使用FindMin[U]扩展AbstractMaxPQ[U]{
...
覆盖def insert[K>:U:Ordering](v:K):ArrayMapQ[U]=???
...
这是我的IDE自动生成的。当然,我需要这个方法来返回ArrayMapq[K]
。
this.type
不接受类型参数。在这里使用更高级的类型也不起作用
def insert[K>:U:Ordering,G[]您实际上可以使用更高级的类型来执行此操作。它被称为
MaxPQ[+U,F[\U]{self:F[\U
def插入[K>:U:排序](v:K):F[K]
}
抽象类AbstractMaxPQ[U:排序,F[]]扩展了MaxPQ[U,F]{
self:F[U]=>
}
类ArrayMapQ[U:Ordering:ClassTag]扩展了AbstractMaxPQ[U,ArrayMapQ]{
覆盖def插入[K>:U:Ordering](v:K):ArrayMapQ[K]=???
}
不要将G
传递到insert
中,而是将其用作类/特征本身的类型参数,然后确保此
扩展了它。这就是self类型的用途(self:F[U]
)。另一个有用的可能您希望返回类型比此更宽。类型
但比MaxPQ[U]
更窄。然后尝试引入类型成员
sealed trait MaxPQ[+U] {
type This <: MaxPQ[U]
// type This >: this.type <: MaxPQ[U] { type This = MaxPQ.this.This }
def insert[K >: U : Ordering](v: K): This
}
abstract class AbstractMaxPQ[U : Ordering] extends MaxPQ[U] {
override type This <: AbstractMaxPQ[U]
// override type This >: this.type <: AbstractMaxPQ[U] { type This = AbstractMaxPQ.this.This }
}
class ArrayMaxPQ[U : Ordering : ClassTag] extends AbstractMaxPQ[U] {
override type This = ArrayMaxPQ[U]
override def insert[K >: U : Ordering](v: K): ArrayMaxPQ[U] = ???
}
this.type
有什么问题?它在具体实现中返回了一个错误的类型:ArrayMapq[U]
而不是ArrayMapq[K]
.Ah是的,最简单的解决方案是将所有这些方法都移动到一个typeclass中。但是我需要使用逆变
类型参数返回当前类型,所以K
@user How?我想这个
(akaF
)受这个.type
的限制是您方法的一个优势(使用类型参数和self-type)。这几乎有效-您还没有看到这部分,这就是原因。我将编辑我的初始问题。简而言之,我还有一个基于AbstractMaxPQ
的混音。根据您的建议,self:AbstractMaxPQ[U,F[\U]=>
无法编译:F[\uq]不接受类型参数,应为:一个
这是我的错误,在FindMIn
中,如果我只做了self:AbstractMaxPQ[U,F]=>
,那么它就工作了
sealed trait MaxPQ[+U] {
type This <: MaxPQ[U]
// type This >: this.type <: MaxPQ[U] { type This = MaxPQ.this.This }
def insert[K >: U : Ordering](v: K): This
}
abstract class AbstractMaxPQ[U : Ordering] extends MaxPQ[U] {
override type This <: AbstractMaxPQ[U]
// override type This >: this.type <: AbstractMaxPQ[U] { type This = AbstractMaxPQ.this.This }
}
class ArrayMaxPQ[U : Ordering : ClassTag] extends AbstractMaxPQ[U] {
override type This = ArrayMaxPQ[U]
override def insert[K >: U : Ordering](v: K): ArrayMaxPQ[U] = ???
}
sealed trait MaxPQ[+U] {
type This[K >: U] <: MaxPQ[K]
// type This[K >: U] <: MaxPQ[K] { type This[K1 >: K] = MaxPQ.this.This[K1] }
def insert[K >: U : Ordering](v: K): This[K]
}
abstract class AbstractMaxPQ[U : Ordering] extends MaxPQ[U] {
override type This[K >: U] <: AbstractMaxPQ[K]
// override type This[K >: U] <: AbstractMaxPQ[K] { type This[K1 >: K] = AbstractMaxPQ.this.This[K1] }
}
class ArrayMaxPQ[U : Ordering : ClassTag] extends AbstractMaxPQ[U] {
override type This[K >: U] = ArrayMaxPQ[K]
override def insert[K >: U : Ordering](v: K): ArrayMaxPQ[K] = ???
}