Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does List.toString and Vector.toString return a nice representation but not Array.toString?

Tags:

scala

scala> Array(1, 2, 3).toString
res1: String = [I@11cf437c

scala> List(1, 2, 3).toString
res2: String = List(1, 2, 3)

scala> Vector(1, 2, 3).toString
res3: String = Vector(1, 2, 3)

Logically, one would expect Array(1, 2, 3).toString to return "Array(1, 2, 3)".

Update: seems to me like Array maps to the built in Java array type—is this correct? and if yes, is this the reason Array.toString has to behave like this?

like image 369
Erik Kaplun Avatar asked Jan 13 '23 10:01

Erik Kaplun


1 Answers

It is because Array is a Java object. You can however use runtime.ScalaRunTime.stringOf if it suits your needs.

scala> runtime.ScalaRunTime.stringOf(Array(1, 2, 3))
res3: String = Array(1, 2, 3)
like image 109
sberry Avatar answered Jan 31 '23 04:01

sberry