Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Array constructor?

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?

like image 815
Łukasz Lew Avatar asked Apr 23 '10 16:04

Łukasz Lew


People also ask

How do I fill an array in Scala?

Use the nextInt() method to fill the array with random values. It generates the random integer values, and to get filled, we used the fill keyword.

Can Scala object have constructor?

Constructors in Scala describe special methods used to initialize objects. When an object of that class needs to be created, it calls the constructor of the class. It can be used to set initial or default values for object attributes.

How do you create an array of objects in Scala?

Creating an Array and Accessing Its ElementsScala translates the first line in the example above into a call to Array::apply(), defined in the Array companion object. Such a method takes a variable number of values as input and creates an instance of Array[T], where T is the type of the elements.

What is a Scala constructor?

Scala constructor is used for creating an instance of a class. There are two types of constructor in Scala – Primary and Auxiliary. Not a special method, a constructor is different in Scala than in Java constructors. The class' body is the primary constructor and the parameter list follows the class name.


2 Answers

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.
like image 114
mipadi Avatar answered Oct 15 '22 02:10

mipadi


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.

like image 40
Rex Kerr Avatar answered Oct 15 '22 04:10

Rex Kerr