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!