Arrays.deepToString and Scala Array

by lukeg   Last Updated August 14, 2019 18:26 PM

Consider the code:

val arr2d = Array(Array(1, 2, 3), Array(4, 5, 6))
util.Arrays.deepToString(arr2d)

It fails with the following error on the second line:

Error:(79, 30) type mismatch;
found : Array[Array[Int]]
required: Array[Object] Note: Array[Int] <: Object, but class Array is invariant in type T. You may wish to investigate a wildcard type such as _ <: Object. (SLS 3.2.10) util.Arrays.deepToString(arr2d)

It has problaby something with type erasure, but I am not exactly sure what to do to make the code compile, despite the compiler trying to suggest a solution. I am even more puzzled because the analogous Java code works (i.e. something along the lines of Arrays.deepToString(new int[][] { { 1, 2, 3}, {4, 5, 6}})).

What is the problem in the code and how can it fixed?

Tags : arrays scala


Related Questions




How to combine arrays of monoid type?

Updated March 07, 2017 13:26 PM