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

发布于 2024-09-08 12:51:15 字数 698 浏览 0 评论 0原文

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 在类声明中提供的隐式参数列表之前插入一个空参数列表?

scalac 源中的注释

// 转换(隐式...)为 ()(隐式...)如果它是唯一的 参数部分

我很想知道为什么这样做。我觉得相当令人惊讶。

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)

Why does Scalac insert an empty parameter list before the implicit parameter list provided in the class declaration?

This seems to be a feature, not a bug, judging by the commentary in the scalac sources:

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

I'm curious to know why this is done. I find it rather surprising.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離殇 2024-09-15 12:51:15

我的看法是隐式参数列表不会取代常规参数列表。由于构造函数定义至少需要一个参数列表,因此如果没有明确指示,则会生成“()”。

虽然这可能确实令人费解,但它与在根本不存在参数列表时生成空构造函数是一致的。

The way I see it is that implicit parameter list does not replace the regular one(s). Since for constructor definitions at least one parameter list is needed, if nothing is indicated explicitly '()' is generated.

While this might be indeed puzzling, it's in line with generating an empty constructor when no parameter lists at all are present.

何时共饮酒 2024-09-15 12:51:15

好的,在@venechka的回答,我想我已经弄清楚了。

对于普通类,Scala 会在类声明 (class B) 或类实例化点(new Anew B):

scala> class A()
defined class A

scala> new A
res19: A = A@12cdd20

scala> new A()
res20: A = A@1c37b8f

scala> class B
defined class B

scala> new B
res21: B = B@12801c5

scala> new B()
res22: B = B@79a340

为了遵循这一原则,它在隐式参数列表(class D(implicit ...))之前推断出一个空参数列表。

scala> class C()(implicit a: Int = 0)
defined class C

scala> new C
res23: C = C@9d1714

scala> new C()
res24: C = C@b38dba

scala> new C()(0)
res25: C = C@1677979

scala> class D(implicit a: Int = 0)
defined class D

scala> new D
res26: D = D@1a0d111

scala> new D()
res27: D = D@14e3372

scala> new D()(0)
res28: D = D@1609872

Okay, with the help of @venechka's answer, I think I've figured it out.

With ordinary classes, Scala infers and empty parameter list, either at the class declaration (class B), or at the point of class instantiation (new A and new B):

scala> class A()
defined class A

scala> new A
res19: A = A@12cdd20

scala> new A()
res20: A = A@1c37b8f

scala> class B
defined class B

scala> new B
res21: B = B@12801c5

scala> new B()
res22: B = B@79a340

So to be in keeping with this principle, it infers an empty parameter list before an implicit parameter list (class D(implicit ...)).

scala> class C()(implicit a: Int = 0)
defined class C

scala> new C
res23: C = C@9d1714

scala> new C()
res24: C = C@b38dba

scala> new C()(0)
res25: C = C@1677979

scala> class D(implicit a: Int = 0)
defined class D

scala> new D
res26: D = D@1a0d111

scala> new D()
res27: D = D@14e3372

scala> new D()(0)
res28: D = D@1609872
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文