Neil Chambers
Neil Chambers

Reputation: 393

scala += assignment oddity on string

I'm trying Scala 2.9 (and I like it!) Consider the following:

scala> "hello" += " world"
<console>:8: error: value += is not a member of java.lang.String
         "hello" += " world"

And now

scala> var h = "hello "
h: java.lang.String = "hello "

scala> h += "world"

scala> h
res24: java.lang.String = hello world

I would have thought that both string expressions in the first example would naturally evaluate in order to allow the operation. Is there a good reason for this behavior?

Cheers!

Upvotes: 2

Views: 186

Answers (1)

trutheality
trutheality

Reputation: 23465

You can't modify a constant. "hello" is constant, h isn't.

You're writing

"hello" = "hello" + " world"

which doesn't make a lot of sense.

Upvotes: 10

Related Questions