English 中文(简体)
Scala 抽象类型界限交叉引用
原标题:Scala abstract type bounds cross referencing

我有两个抽象的分类, 彼此互相保留引用。 如何/ 能够将类型成员捆绑起来, 使衍生的十六进制类的SideT成员中HexT的类型永远是衍生的十六进制类? 因此, 对于导出类的六进制A, 保证 : HexA# SideT# HexT = 六进制A

类似地,所有衍生的子类的HexT成员中的SideT类型将是衍生的副类:SideB#HexT#SideT=SideB

我使用 Scala 来表示 Eclipse 2.1.0. M1 和 Eclipse 3.7.2 。 所有班级都在单独的文件中。 以下编译为 " 确定 ", 但不保证我要的东西:

abstract class Hex { type SideT <: Side {type HexT <= Hex } }
abstract class Side { type HexT <: Hex {type SideT <= side } }
class HexC() extends Hex() { type SideT = SideC }
class SideC extends Side { type HexT = HexC }

但以下内容无法在衍生的实施工作中加以汇编:

abstract class Hex{type SideT <: Side {type HexT = this.type}}
abstract class Side{type HexT <: Hex {type SideT = this.type}}
class HexC() extends Hex(){
  type SideT = SideC //This won t compile
}
class SideC extends Side {
  type HexT = HexC //this won t compile
}

这是否正确?

最佳回答

这个怎么样?

abstract class Hex {
  type SideT <: Side
}

abstract class Side {
  type HexT <: Hex
}

class HexC extends Hex {
  type SideT = SideC
}

class SideC extends Side {
  type HexT = HexC
}

val evidence1 = implicitly[SideC#HexT =:= HexC]
val evidence2 = implicitly[SideC#HexT#SideT =:= SideC]

或带有域的封装特性:

trait abstractDomain {
  type SideT <: Side
  type HexT <: Hex

  abstract class Hex
  abstract class Side
}

object domain extends abstractDomain {
  type SideT = SideC
  type HexT = HexC

  class HexC extends Hex
  class SideC extends Side
}

或带有类型参数:

abstract class Hex[HexT <: Hex[HexT, SideT], SideT <: Side[HexT, SideT]]
abstract class Side[HexT <: Hex[HexT, SideT], SideT <: Side[HexT, SideT]]

class HexC extends Hex[HexC, SideC]
class SideC extends Side[HexC, SideC]
问题回答

暂无回答




相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签