Question

To instantiate the variables can do so:

scala> var (a, b, c) = (0, 0, 23)
a: Int = 0
b: Int = 0
c: Int = 23

but if I wanted to do such a thing?

scala> a = b = c
<console>:10: error: type mismatch;
 found   : Unit
 required: Int
       a = b = c
             ^

how can I do?

Thanks

Was it helpful?

Solution 2

You can't do a = b = c because a has already been defined as a Int var, and with the a = b = c statement you are giving a a Unit, 'b = c'.

When you assign a value to a variable in Scala you don't get as a result the value assigned.

In other languages b = c would be evaluated to 23, the value of c. In Scala b = c is just a Unit, writing a = b = c is exactly like writing a = (b = c), hence the error.

OTHER TIPS

var a,b,c = 0

should do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top