scala – 公开来自单例类型的路径依赖类型
发布时间:2020-12-16 18:29:32 所属栏目:安全 来源:网络整理
导读:我正在尝试让 Scala为来自单例类型的路径依赖类型找到正确的类型. 首先,这是示例的类型容器,以及一个实例: trait Container { type X def get(): X}val container = new Container { type X = String def get(): X = ""} 我可以在第一次尝试中看到String(所
我正在尝试让
Scala为来自单例类型的路径依赖类型找到正确的类型.
首先,这是示例的类型容器,以及一个实例: trait Container { type X def get(): X } val container = new Container { type X = String def get(): X = "" } 我可以在第一次尝试中看到String(所以我已经有了一个工作场景): 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-internals上的
this个帖子,特别是Adriaan的解释.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |