10

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?

1

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
@PeWu +1 for having the phrase "it's not implicitly implicit without the implicit keyword" :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.