docs.scala-lang icon indicating copy to clipboard operation
docs.scala-lang copied to clipboard

Scala 3 book: Create integer and string variables redefines a immutable variable

Open VivCh14 opened this issue 6 months ago • 1 comments

The example at create-integer-and-string-variables, for Scala uses immutable variable x to re-define it from Int to a String type. Solution would be to either use another variable say z or make the x variable of type var that will make it mutable.

val x = 1
val z = "Hi"
val y = """foo
           bar
           baz"""

or

var x = 1
var x = "Hi"
val y = """foo
           bar
           baz"""

VivCh14 avatar May 13 '25 13:05 VivCh14

it was probably a mistake to reuse x so x, y, z makes sense

bishabosha avatar May 13 '25 14:05 bishabosha