In Scala, why does Array(“1”) ++ “-3” output Array[Any] = Array(1, -, 3)?

  • A+
Category:Languages

Why in Scala 2.12.6 does Array("1") ++ "-3" output res1: Array[Any] = Array(1, -, 3)?

How do I get the result Array("1", "-3")?

 


The ++ operator concatenates two Lists (or other collections) together. As a String is essentially just a list of Chars, as @senjin.hajrulahovic points out the compiler treats it this way (and returns an Array[Any].

You can get around this in multiple ways. For example, Array("1") ++ Array("-3") (to add two arrays together) or Array("1") :+ "-3" to put the value on the end of the Array.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: