Scala behaviour when assigning literals or variables to Char

Viewed 266

In Scala, if I have a variable declaration, e.g.

var x: Char = 'a'

If I then try and update this character by adding 1, e.g.

x = x + 1

I get a compilation error: Type mismatch, found Int required Char. However, I can do this without a compilation error:

x = 'a' + 1

I'm guessing this has something to do with literal values vs objects, however, I'm trying to get my head around the exact behaviour. You can clearly assign a literal integer to a Char, e.g. 97, and you can also assign the result of 97-32. However if I say 97-32+5 then I get a type mismatch error. At what point does the compiler differentiate between an expression that results in a literal vs one that result in an object?

2 Answers
Related