Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala behaviour when assigning literals or variables to Char

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?

like image 310
Tranquility Avatar asked Aug 03 '17 14:08

Tranquility


People also ask

Can I assign number to char?

You can assign a single Char in a Text or Code variable to a Char variable, as shown in the second line of the following code example. You can assign a numeric value to a Char variable, as shown in the third line of the following code example.

What are literals in Scala?

The literals are a series of symbols utilized for describing a constant value in the code. There are many types of literals in Scala namely Character literals, String literals, Multi-Line String literals, Boolean literals, Integer literals, and Floating point literals.

What is a char in Scala?

Scala Char |(x: Char) method with example The |(x: Char) method is utilized to find the bit-wise OR of the stated character value and given 'x' in the argument list. Method Definition: def|(x: Char): Int. Return Type: It returns the bit-wise OR of the stated character value and given 'x'. Example: 1#

What is a literal data type?

A literal (or literal data) is data that appears directly in the source code, like the number 5, the character A, and the text “Hello, World.” A value is an immutable, typed storage unit. A value can be assigned data when it is defined, but can never be reassigned. A variable is a mutable, typed storage unit.


1 Answers

Assignment is the key here. Look at the following REPL session:

alex@POSITRON ~ $ scala
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val x:Char = 'a'
x: Char = a

scala> x + 1
res0: Int = 98

scala> var y:Char = 'a'
y: Char = a

scala> y + 1
res1: Int = 98

scala> y = y + 1
<console>:8: error: type mismatch;
 found   : Int
 required: Char
       y = y + 1
             ^

scala> 

So as you can see unless you try to reassign the variable values everything goes fine. When you write 'a'+1 or x + 1 it gets converted to Int. So when you finally try to x = x + 1 reassign then you are trying to assign Int value to the Char variable. This explain why compilation error occurs.

In the Char companion object there is implicit def char2int(x: Char): Int method. I think in var x:Char = 'a' + 1 the first thing which happens is invocation of this method to convert 'a' to 97. Then 97 is added 1, both as Ints. Then the variable x gets instantiated in the same way as in val y:Char = 98. This I think explains how variable initialization works.

like image 79
Alexander Arendar Avatar answered Oct 16 '22 22:10

Alexander Arendar