njk2015
njk2015

Reputation: 543

Multi variable assignment in Scala

I am reading a book in scala and one of the exercises is as follows:

Come up with one situation where the assignment x = y = 1 is valid in Scala. (Hint: Pick a suitable type for x.)

The 2 solutions I could come up with are:

val x, y : Int = 1
val x, y = (1, 2)

Have I missed another way that the exercise is looking for?

Upvotes: 0

Views: 66

Answers (1)

Dima
Dima

Reputation: 40500

"valid" and "useful" don't necessarily mean the same thing :)

 scala> var y = 2
 y: Int = 2

 scala> val x = y = 1
 x: Unit = ()

 scala> 

Upvotes: 1

Related Questions