njk2015 njk2015 - 7 months ago 41
Scala Question

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?

Answer Source

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

 scala> var y = 2
 y: Int = 2

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download