scala> val a = Array [Double] (10)
a: Array[Double] = Array(10.0)
scala> val a = new Array [Double] (10)
a: Array[Double] = Array(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
Why these two expressions have different semantics?
scala> val a = Array [Double] (10)
a: Array[Double] = Array(10.0)
scala> val a = new Array [Double] (10)
a: Array[Double] = Array(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
Why these two expressions have different semantics?
It's a bit confusing, but Scala has the notion of classes which you can create instances of, and objects, which are basically singleton instances of a class. It also has the notion of companion classes, which is a pair of a class and an object with the same name. This mechanism allows a "class" to essentially have static methods, which are otherwise not possible in Scala.
Array
has both a class and a companion object. Furthermore, the Array
object has an apply
method. apply
means you can create an object with Array(arg)
. But because Array
is a companion class, it also has a constructor that can be called via the more usual mechanism of new Array(arg)
.
The issue is that apply
in the the Array
object has different semantics than the Array
constructors. The apply
method creates an array out of the specified objects, so, for example, Array(1,2,3)
returns an array consisting of the objects 1
, 2
, and 3
. The constructors, on the other hand, take arguments that specify the size of the dimensions of the array (so you can create multidimensional arrays), and then initialize all slots to a default value.
So, basically:
val a = Array [Double] (10)
calls the apply
method on the Array
object, which creates a new array containing the given objects.val a = new Array [Double] (10)
calls the Array
constructor, which creates a new array with 10 slots, all initialized to a default value of 0.0
.new Array[Double](10)
is supposed to be equivalent to new double[10]
in Java.
But Scala also provides convenience methods on the singletons corresponding to its collection classes, and Array
is no exception.
Thus, if you can say List(1,2,3,4,5)
it seems natural that you could also say Array(1,2,3,4,5)
. And you can.
But it does leave one in the slightly awkward position of having rather different results depending on whether one adds the word new
or not. Given the competing interests, I think it's the best solution overall, but it does take a little getting used to.