4
虽然我知道有更多的这样做的idomatic方式,为什么不这个代码工作? (主要是,为什么第一次尝试只是x += 2
工作。)这些看起来很奇特(至少对Scala来说是个新手)错误消息一些implicit def
魔法不能正常工作?为什么+ =不适用于列表?
scala> var x: List[Int] = List(1)
x: List[Int] = List(1)
scala> x += 2
<console>:7: error: type mismatch;
found : Int(2)
required: String
x += 2
^
scala> x += "2"
<console>:7: error: type mismatch;
found : java.lang.String
required: List[Int]
x += "2"
^
scala> x += List(2)
<console>:7: error: type mismatch;
found : List[Int]
required: String
x += List(2)