YoBre
YoBre

Reputation: 2530

How to assign the same value to more var in scala

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

Upvotes: 1

Views: 305

Answers (2)

user908853
user908853

Reputation:

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.

Upvotes: 2

My other car is a cadr
My other car is a cadr

Reputation: 1431

var a,b,c = 0

should do the trick.

Upvotes: 5

Related Questions