是否有可能实现类型推断?

时间:2013-07-03 06:25:20

标签: scala type-conversion traits

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

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 [???])?

1 个答案:

答案 0 :(得分: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