误导编译时错误

mis*_*tor 1 scala compiler-errors

为什么以下编译?

scala> val ch1 = 'a' + 'b'
ch1: Int = 195
Run Code Online (Sandbox Code Playgroud)

但以下不是?

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

scala> ch1 += 'b'
<console>:9: error: type mismatch;
 found   : Int
 required: Char
       ch1 += 'b'
           ^

scala> ch1 = ch1 + 'b'
<console>:8: error: type mismatch;
 found   : Int
 required: Char
       ch1 = ch1 + 'b'
                 ^
Run Code Online (Sandbox Code Playgroud)

为什么错误信息如此误导?为什么它说required: Char我什么时候通过显然是一个Char

n. *_* m. 7

当你添加一个Char和另一个时Char,结果是一个Int.

scala> 'a' + 'c'      
res2: Int = 196
Run Code Online (Sandbox Code Playgroud)

这是错误消息的"找到"部分.