My code looks like this:
val people = Array(Array("John", "25"), Array("Mary", "22"))
val headers = Seq("Name", "Age")
val myTable = new Table(people, headers)
I get this syntax error:
overloaded method constructor Table with alternatives:
(rows: Int,columns: Int)scala.swing.Table
<and>
(rowData: Array[Array[Any]],columnNames: Seq[_])scala.swing.Table
cannot be applied to
(Array [Array[java.lang.String]], Seq[java.lang.String])
I don’t see why the second alternative isn’t used. Is there a distinction between “Any” and “_” that’s tripping me up here?
As Kim already said, you need to make your array covariant in his element type, because Scala’s Arras are not covariant like Java’s/C#’s.
This code will make it work for instance:
This just tells the compiler that
Tshould be covariant (this is similar to Java’s? extends Tor C#’sout T).If you need more control about what types are allowed and which not, you can also use:
This will tell the compiler that
Tcan be any subtype ofAny(which can be changed fromAnyto the class you require, likeCharSequencein your example).Both cases work the same in this scenario:
Edit: If the class in question is not in your control, declare the type you want explicitly like this:
Update
This is the source code in question:
I wonder if someone forgot to remove the workaround, because #2005 is fixed since May 2011 …