【发布时间】: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