score:2

Accepted answer

there's a difference between mutable data structures and mutable references - see this answer for details.

in this particular case, you're using mutable reference to immutable data structure, which means you can only replace it with completely different one. this would work:

var pair = (99, "luftballons") 
println(pair._1) // ok
pair = (100, "luftballons") // ok

as others already pointed out there's a convenience method copy defined for tuple, which allows creating a copy of an object (potentially replacing some fields).

pair = pair.copy(5, "kittens") // ok

score:1

you have to update your pair like this:

pair = (89, pair._2)
pair: (int, string) = (89,luftballons)

by a new assignment to the pair, not to the underlying tuple. or you use pair.copy, like suggested by chengpohi.

scala> pair = pair.copy(_1=101)
pair: (int, string) = (101,luftballons)

Related Query

More Query from same tag