Why can't the first parameter list of a class be implicit?

前端 未结 2 1092
无人共我
无人共我 2020-12-15 19:39
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         


        
相关标签:
2条回答
  • 2020-12-15 20:12

    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.

    0 讨论(0)
  • 2020-12-15 20:26

    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
    
    0 讨论(0)
提交回复
热议问题