开发者

Two sets of constructor parameters in a scala class

开发者 https://www.devze.com 2023-01-09 09:17 出处:网络
What does this code do? Why is there two sets of constructor parameters? class A(val x: Int)(val y: Int)

What does this code do? Why is there two sets of constructor parameters?

class A(val x: Int)(val y: Int)

I can initialize an object and use both fields:

val a = new A(5)(7)
println(a.x + ", " + a.y)

If I make it a case class, I can match only by the first set of parameters.

case class A(x: Int)(y: Int)
val a = A(5)(7)
a match {
  A(x) => println(x)
}

It's not possible to create 3 sets of parameter开发者_开发知识库s. It doesn't compile. So what is the meaning of the two sets of constructor parameters?


According to the scala specification (see section 5.3), the second set of parameters is dedicated to implicit parameters. Dividing the parameters in two sets allow you to define only non-implicit paameter and let the other be contextually defined.

It is quite strange actually that the compiler accpet non-implicit parameters in the second set.

0

精彩评论

暂无评论...
验证码 换一张
取 消