scala 中参数化方法的类型覆盖

发布于 2024-12-18 10:22:24 字数 1255 浏览 1 评论 0原文

我尝试在 scala 中重写我的参数化方法,

我有一个像这样的抽象类:

abstract class Ranking[G <: AbstractGenome, MG <: MultiGoalLike] {
  def operate(individuals :IndexedSeq [IndividualMG[G,MG]]):IndexedSeq [IndividualMG[G,MG]
}

我想要两种类型的排名,需要 IndividualMG 的装饰版本:IndividualMG[G,MG] 与 IDistance< /code> 和 IndividualMG[G,MG] 与 IRank

我的 IndividualMG 类签名:

class IndividualMG[G <: AbstractGenome,MG <: MultiGoalLike] (val genome: G,val multiGoal:MG) 

我的两个排名类:

class Ranking1 [G <: AbstractGenome, MG <: MultiGoalLike] extends Ranking[G,MG] {
override def operate(individuals :IndexedSeq [IndividualMG [G,MG] with IRanking])
:IndexedSeq [IndividualMG [G,MG] with IRanking]= {
return ...
}

class Ranking2 [G <: AbstractGenome, MG <: MultiGoalLike] extends Ranking[G,MG] {
override def operate(individuals :IndexedSeq [IndividualMG [G,MG] with IDistance])
:IndexedSeq [IndividualMG [G,MG] with IDistance] = {
return ...
}

我有一个错误,这是逻辑,因为当我尝试覆盖时类型不同,但我如何验证继承类型 [I :IndividualMG [G,MG]]对于我在类Ranking1Ranking2中的两个排名运算符?

谢谢堆栈, SR。

I try to override my parametrized method in scala,

I have an abstract class like this :

abstract class Ranking[G <: AbstractGenome, MG <: MultiGoalLike] {
  def operate(individuals :IndexedSeq [IndividualMG[G,MG]]):IndexedSeq [IndividualMG[G,MG]
}

I want two type of ranking which need decorated version of IndividualMG : IndividualMG[G,MG] with IDistance, and IndividualMG[G,MG] with IRank.

My IndividualMG class signature :

class IndividualMG[G <: AbstractGenome,MG <: MultiGoalLike] (val genome: G,val multiGoal:MG) 

My two class for ranking :

class Ranking1 [G <: AbstractGenome, MG <: MultiGoalLike] extends Ranking[G,MG] {
override def operate(individuals :IndexedSeq [IndividualMG [G,MG] with IRanking])
:IndexedSeq [IndividualMG [G,MG] with IRanking]= {
return ...
}

class Ranking2 [G <: AbstractGenome, MG <: MultiGoalLike] extends Ranking[G,MG] {
override def operate(individuals :IndexedSeq [IndividualMG [G,MG] with IDistance])
:IndexedSeq [IndividualMG [G,MG] with IDistance] = {
return ...
}

I have an error, it's logic because type differs when i try to override, but how can i make to verify inheritance type [I <: IndividualMG [G,MG]] for my two ranking operator in class Ranking1 and Ranking2 ?

Thanks stack,
SR.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左秋 2024-12-25 10:22:24

怎么样

abstract class Ranking[
    -I <: IndividualMG[G, MG], 
    G <: AbstractGenome, 
    MG <: MultiGoalLike] {
  def operate(individuals :IndexedSeq[I])
}

class Ranking1[
   G <: AbstractGenome, 
   MG <: MultiGoalLike] 
  extends Ranking[IndividualMG[G,MG] with IDistance, G, MG] ...

What about

abstract class Ranking[
    -I <: IndividualMG[G, MG], 
    G <: AbstractGenome, 
    MG <: MultiGoalLike] {
  def operate(individuals :IndexedSeq[I])
}

class Ranking1[
   G <: AbstractGenome, 
   MG <: MultiGoalLike] 
  extends Ranking[IndividualMG[G,MG] with IDistance, G, MG] ...
故乡的云 2024-12-25 10:22:24

我认为您正在寻找路径依赖类型

abstract class AbstractGenome
class ConcreteGenome extends AbstractGenome
class MultiGoalLike
trait IRanking

abstract class Ranking[G <: AbstractGenome, MG <: MultiGoalLike] {
  type RankType <: IndividualMG[G,MG]
  def operate(individuals :IndexedSeq [RankType])
}

class IndividualMG[G <: AbstractGenome,MG <: MultiGoalLike] (val genome: G,
                                                          val multiGoal:MG)

class Ranking1 [G <: AbstractGenome, MG <: MultiGoalLike] extends Ranking[G,MG] {
  type RankType = IndividualMG [G,MG] with IRanking
  override def operate(individuals :IndexedSeq [RankType])= println("it worked!")
}

val cg = new ConcreteGenome
val mgl = new MultiGoalLike

val r1 = new Ranking1[ConcreteGenome, MultiGoalLike]

val i1 = new IndividualMG(cg, mgl)
val i2 = new IndividualMG(cg, mgl) with IRanking

r1.operate(Vector(i1)) // error
r1.operate(Vector(i2)) // OK

I think you're looking for a Path dependent type.

abstract class AbstractGenome
class ConcreteGenome extends AbstractGenome
class MultiGoalLike
trait IRanking

abstract class Ranking[G <: AbstractGenome, MG <: MultiGoalLike] {
  type RankType <: IndividualMG[G,MG]
  def operate(individuals :IndexedSeq [RankType])
}

class IndividualMG[G <: AbstractGenome,MG <: MultiGoalLike] (val genome: G,
                                                          val multiGoal:MG)

class Ranking1 [G <: AbstractGenome, MG <: MultiGoalLike] extends Ranking[G,MG] {
  type RankType = IndividualMG [G,MG] with IRanking
  override def operate(individuals :IndexedSeq [RankType])= println("it worked!")
}

val cg = new ConcreteGenome
val mgl = new MultiGoalLike

val r1 = new Ranking1[ConcreteGenome, MultiGoalLike]

val i1 = new IndividualMG(cg, mgl)
val i2 = new IndividualMG(cg, mgl) with IRanking

r1.operate(Vector(i1)) // error
r1.operate(Vector(i2)) // OK
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文