Why can't the first parameter list of a class be implicit?
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.
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.
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
精彩评论