【问题标题】:Exposing a path-dependent type coming from a singleton type暴露来自单例类型的路径相关类型
【发布时间】:2012-02-22 08:00:57
【问题描述】:

我正在尝试让 Scala 为来自单例类型的路径相关类型找到正确的类型。

首先,这里是示例的类型容器,以及一个实例:

trait Container {
  type X
  def get(): X
}

val container = new Container {
  type X = String
  def get(): X = ""
}

我可以在第一次尝试中看到字符串(所以我已经有了一个工作场景):

class WithTypeParam[C <: Container](val c: C) {
  def getFromContainer(): c.X = c.get()
}

val withTypeParam = new WithTypeParam[container.type](container)

// good, I see the String!
val foo: String = withTypeParam.getFromContainer()

但是当没有类型参数时,这不再起作用了。

class NoTypeParam(val c: Container) {
  def getFromContainer(): c.X = c.get()
}

val noTypeParam = new NoTypeParam(container)

// this does *not* compile
val bar: String = noTypeParam.getFromContainer()

有人知道为什么需要类型参数吗?

【问题讨论】:

    标签: scala type-parameter path-dependent-type dependent-method-type


    【解决方案1】:

    请参阅 scala-internals 上的 this 线程,特别是 Adriaan 的解释。

    【讨论】:

      猜你喜欢
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      相关资源
      最近更新 更多