ihainan
ihainan

Reputation: 75

what is the difference between Array and Array[Type] in Scala?

I tried to compile and run the following Scala source code:

class People(val name: String)
class Student(studentName: String) extends People(studentName)

def getNames(array: Array[People]) = { array.map(_.name) }

getNames(Array[Student](new Student("Mike"), new Student("Tom")))

I got error message:

Name: Compile Error
Message: <console>:30: error: type mismatch;
 found   : Array[Student]
 required: Array[People]
Note: Student <: People, but class Array is invariant in type T.
You may wish to investigate a wildcard type such as `_ <: People`. (SLS 3.2.10)
       getNames(Array[Student](new Student("Mike"), new Student("Tom")))

It's as expected because Array[Student] is not a subtype of Array[People].

Then I updated

getNames(Array[Student](new Student("Mike"), new Student("Tom")))

to

getNames(Array(new Student("Mike"), new Student("Tom")))

The error was gone. So I'm wondering in Scala what is the difference between Array and Array[Type], especially when it's passed as a method parameter.

Thanks in advance!

Upvotes: 3

Views: 313

Answers (1)

Benjamin Gruenbaum
Benjamin Gruenbaum

Reputation: 276536

This is because Scala arrays don't repeat the covariance mistake of Java. In Java you can do:

Student[] students = new Student[10];
Person[] people = students; // ok in Java arrays are covariant
people[1] = new Person(); // RuntimeException in Java!!

Scala is safer than that (like Java generic collections).

You should always use the specific version of collections in order to get better type safety. Your Array example detects the generic parameter from the function call - which is Array[Student].

Upvotes: 1

Related Questions