views:

85

answers:

1

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 parameters. It doesn't compile. So what is the meaning of the two sets of constructor parameters?

+5  A: 

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.

Nicolas
Thanks. If I add the _implicit_ keyword into the second set, it starts to be implicit, but it's not implicitly implicit without the _implicit_ keyword.Anyway, the scala specification does not allow non-implicit parameters in the second set.
PeWu
Well... this section of the spec seems very strange: the example given right below the grammar does not seem to correspond to the gramar. And "class Test (a:Int)(b:Int)(c:Int)" is a valid class definition.
Nicolas
@PeWu +1 for having the phrase "it's not implicitly implicit without the implicit keyword" :)
I82Much