0

I just learned Scala.

When I read official documentation to look up Array, for example, it has two versions. One is for Class, the other is for Object. I think I know the difference between Class and Object and when I need find methods of Array, I usually go to the Class version. I do not quite know when I need to go to this Object version.It seems when I want to create Multidimensional arrays, I need method(ofDim) in Object version instead of class version. Why?

Community
  • 1
  • 1
CSnerd
  • 2,129
  • 8
  • 22
  • 45

3 Answers3

7

The identifier Array refers to either the type or its companion object, depending on context.

scala> Array('a', 'b', 'c')
res0: Array[Char] = Array(a, b, c)

res0 is an instance of the Array type. res0 is an object.

scala> Array
res1: Array.type = scala.Array$@1a69136

res1 is the companion object. res1 is an object. It is a singleton, meaning that there are no other objects of its type.

These two objects have different methods on them, because they're very different things.

Instances of the Array type have the methods defined by the class. These are, naturally, the methods that operate on a particular Array instance. For example, the length method returns the length of the array. You need an instance to do this. It wouldn't make sense to write Array.length because that doesn't specify which array you want the length of. But Array('a', 'b', 'c').length is 3.

The companion object has the methods defined by the object. These are the methods that do not require an Array instance. Companion objects will object contain methods that create instances, as is the case for Array. Hence Array.ofDim(2, 2) creates a 2x2 array. That method isn't defined by the class because instances shouldn't have it. For example, it wouldn't make much sense to write Array('a', 'b', 'c').ofDim(2, 2), because the result (an empty 2x2 array) would have nothing to do with the instance that the method was called upon.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
  • 1
    can I say that the methods defined by the object are similar to [static methods](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html) in Java? – CSnerd Dec 07 '14 at 03:08
  • Yes, very much so. If you'd mentioned you were familiar with Java, I'd have just said that ;) – Chris Martin Dec 07 '14 at 03:11
0

As per my understanding, the class defines the actual implementation with methods to operate on the instances (in this case Array - methods like map, count, fold etc.).

The companion object on the other hand provides utility methods for creation (like apply...) (for Array in this case it mostly contains factory methods).

nitishagar
  • 9,038
  • 3
  • 28
  • 40
0

The class methods operate on the class instance data. Like elements of an array for the Array class. The companion object methods usually operate on multiple instances of a class or create one.

The companion objects in Scala play the same role as static members in Java or C#. For example, for Array it mostly contain factory methods. I would not expect to see methods like length or isEmpty on the companion object and on the other hand I would expect to see factory methods on the instance of a class.

Dennis
  • 2,615
  • 2
  • 19
  • 20