加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

scala – 引用由抽象类型成员隐藏的泛型类型参数

发布时间:2020-12-16 08:44:24 所属栏目:安全 来源:网络整理
导读:有没有办法引用由同名抽象类型成员遮蔽的泛型类型参数? 假设我们有一个特征和扩展类: trait Foo { type T var get: T = _}class Bar[X] extends Foo { override type T = X //We can set the type member by referencing the generic parameter X}class Ba
有没有办法引用由同名抽象类型成员遮蔽的泛型类型参数?

假设我们有一个特征和扩展类:

trait Foo {
    type T
    var get: T = _
}

class Bar[X] extends Foo {
    override type T = X //We can set the type member by referencing the generic parameter X
}

class Baz[T] extends Foo {
    override type T = ??? //How can we reference the generic parameter T here?
}

显然,将泛型参数命名为与类型成员不同通常并不是什么大不了的事,但这并不总是最佳选择(例如,使用外部库时).有没有办法引用阴影泛型参数?在Scala规范中,类型成员和泛型参数之间的这种交互是什么?

解决方法

即使没有覆盖特征的抽象成员,类型T也将始终引用继承的类型成员,而不是泛型参数.这是一个已知的问题,提交为 SI-8421.从提议的修复程序,它解释了为什么这是一个错误:

When binding a name to a symbol,we search up through the context
chain. At each step,we first look in the current scope. If that
fails,we look for members of the prefix of the closest enclosing
context associated with a Template or PackageDef.

However,this means we will look for an inherited type member before
finding an eponymous class type parameter,which is in the scope of an
enclosing context two levels out.

所以也许在问题解决后(标记为2.12.0-M2),即使抽象类型成员被覆盖,也可以引用阴影类型.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读