Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

scala

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")?

like image 943
Archer Avatar asked Oct 10 '18 11:10

Archer


People also ask

Is array a collection in Scala?

Scala provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

What is an array in Scala?

Array is a special kind of collection in scala. it is a fixed size data structure that stores elements of the same data type. The index of the first element of an array is zero and the last element is the total number of elements minus one. It is a collection of mutable values.

What does :+ mean in Scala?

On Scala Collections there is usually :+ and +: . Both add an element to the collection. :+ appends +: prepends. A good reminder is, : is where the Collection goes. There is as well colA ++: colB to concat collections, where the : side collection determines the resulting type.


3 Answers

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.

like image 111
James Whiteley Avatar answered Sep 20 '22 22:09

James Whiteley


++ is binary operator(also called method) that is used to concatenate two collections.eg. Arrays or Lists. If the first operand is an Array, the result defaults to Array. If the first operand is a List, the result defaults to List.

The compiler, by default, treats "-3" as String. All Strings can be treated as collections also (by implicit conversions when required ). But what type of collection? Is it Array? Is it List? depends on its usage with the type of collection it is operated on.

For Example, While using it as Array("1") ++ "-3", the String "-3" by default will be treated by the compiler as Array[Char] because the first operand of ++ is an Array ( though the type of elements will be always Char by default , by the compiler) While using it as List("1") ++ "-3", the String "-3" by default will be treated as List[Char] because the first operand of ++ is a List ( though the type of elements will be always Char by default , by the compiler)

When the ++ operator encounters both operands are collections of different types of elements, their closest super-type in type-hierarchy, will be the type of elements of the resultant collection. The super-type of String, which is the type of elements of first operand(collection) and Char, which is the type of elements of second operand(collection) is Any. So the result of Array("1") ++ "-3" is Array[Any].

If you want to add the String "-3" as an element to the collection, Array("1"), then you must add it as an element to this collection. There is another method ie., :+ or +: to append or prepend an element to the collection as below.

To append to the collection:

scala> Array("1") :+ "-3"
res6: Array[String] = Array(1, -3)

To prepend to the collection:

scala> "-3" +: Array("1")
res7: Array[String] = Array(-3, 1)

Another way of doing the above is as below(converting element to a collection and using ++ method):

scala> Array("1") ++ Array("-3")
res9: Array[String] = Array(1, -3)

scala> Array("-3") ++ Array("1")
res10: Array[String] = Array(-3, 1)

scala>
like image 20
RAGHHURAAMM Avatar answered Sep 22 '22 22:09

RAGHHURAAMM


Because scala treats "-3" as a sequence of chars '-' and '3'.

If you want to append the string "-3" as one element use :+.

like image 43
senjin.hajrulahovic Avatar answered Sep 19 '22 22:09

senjin.hajrulahovic