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

为什么重载具有不同上限的多态方法无法在Scala中编译

发布时间:2020-12-16 19:07:52 所属栏目:安全 来源:网络整理
导读:为什么以下不能在 Scala中编译: class Aclass Bobject X { def f[Q : A](q: Q): Q = q def f[Q : B](q: Q): Q = q} 有错误消息 console:16: error: method f is defined twice conflicting symbols both originated in file 'console' def f[Q : B](q: Q):
为什么以下不能在 Scala中编译:

class A
class B

object X {
  def f[Q <: A](q: Q): Q = q
  def f[Q <: B](q: Q): Q = q
}

有错误消息

<console>:16: error: method f is defined twice
  conflicting symbols both originated in file '<console>'
         def f[Q <: B](q: Q): Q = q

根据我的理解,在类型擦除之后,def f [Q<:A](q:Q):Q应该用其上限替换:def f(q:A):Any和第二个重载f相应地.所以在类型擦除后它们应该是可区分的. 那么为什么Scala还在抱怨?

解决方法

重新发布评论作为提高可见性的答案.

我发现这篇关于似乎是同一个问题的老帖子:http://www.scala-lang.org/old/node/4625.html

这似乎是Scala编译器的一个已知问题,因为在不破坏编译器的其他(仅限Scala)功能和保证的情况下很难支持此功能这一事实.该帖子也显示了一些解决方法.

如果SO上的任何编译器专家能够阐明Dotty是否有用 – 或者我应该说Skala,那将是非常有趣的?

(编辑:李大同)

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

    推荐文章
      热点阅读