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

scala – 为什么类的第一个参数列表不能是隐式的?

发布时间:2020-12-16 09:36:30 所属栏目:安全 来源:网络整理
导读:scala class A(implicit a: Int);defined class Ascala class B()(implicit a: Int);defined class Bscala new A()(1)res1: A = A@159d450scala new B()(1)res2: B = B@171f735scala new A(1)console:7: error: too many arguments for constructor A: ()(im
scala> class A(implicit a: Int);
defined class A

scala> class B()(implicit a: Int);
defined class B

scala> new A()(1)
res1: A = A@159d450

scala> new B()(1)
res2: B = B@171f735

scala> new A(1)
<console>:7: error: too many arguments for constructor A: ()(implicit a: Int)A
       new A(1)

为什么Scalac在类声明中提供的隐式参数列表之前插入一个空参数列表?

这似乎是一个功能,而不是一个bug,由commentary in the scalac sources判断:

// convert (implicit … ) to
()(implicit … ) if its the only
parameter section

我很好奇知道为什么这样做。我觉得很奇怪

解决方法

我看到的方式是隐式参数列表不能替代常规的参数。由于对于构造函数定义,至少需要一个参数列表,如果没有明确指出“()”。

虽然这可能确实令人困惑,但是当没有参数列表存在时,它与生成空构造函数一致。

(编辑:李大同)

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

    推荐文章
      热点阅读