5
シングルトンタイプから来たパス依存型の正しい型をスカラーが見つけようとしています。シングルトンタイプからのパス依存型の公開
まず、ここでは例えば型容器であり、1例:
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()
なぜ型パラメータが必要なのか分かりませんか?