0

考虑以下代码(不可编译):

trait Entity {
    type T <: Reality
    def liveIn: Option[Class[_ <: Unit#T]]
}

abstract class World extends Entity {
    def liveIn = None
}
class Universe extends World { type T = Real.type }
class ParallelUniverse extends World { type T = Unreal.type }

abstract class Humanoid {
    def liveIn = Some(classOf[World#T])
}
class Human extends Humanoid { type T = Real.type }
class Alien extends Humanoid { type T = Unreal.type }

sealed trait Reality
case object Real extends Reality
case object Unreal extends Reality

是否可以在 Some(classOf[???]) 处推断或以某种方式隐式放置具体的类实例?

4

1 回答 1

0

似乎我的问题以其他方式解决了:

trait Entity[T <: Reality] {
  def liveIn: Option[Class[_ <: Entity[T]]]
}

abstract class World[T <: Reality] extends Entity[T] {
  def liveIn = None
}
class Universe extends World[Real.type]
class ParallelUniverse extends World[Unreal.type]

abstract class Humanoid[T <: Reality] extends Entity[T] {
  def liveIn = Some(classOf[World[T]])
}
class Human extends Humanoid[Real.type]
class Alien extends Humanoid[Unreal.type]

sealed trait Reality
case object Real extends Reality
case object Unreal extends Reality
于 2013-07-11T22:10:12.867 回答