0

Update: I had misunderstood an error message, so this question is invalid.

val inputIds = Array[Any](7, 1, 3)

is sufficient to create an Array[Any] in Scala.

===========

I'm trying to create an array that needs to be passed to a method that takes an Array[Any], but the values are numeric.

At first I tried just the normal way of creating an array:

val inputIds = Array(7, 1, 4)

and got the error:

Type mismatch, expected: Array[Any], actual: Array[Int]

(from the IntelliJ editor, I guess I haven't checked if it's correct)

Then I tried creating Integer values directly, e.g. by:

Integer.getInteger("7")

and passing those into the Array constructor, but I kept getting the same error.

Now I tried:

val inputIds: Array[Any] = Array[Any](7, 1, 4)

and it gave me:

Type mismatch, expected: Array[Any], actual: Array[Array[Int]]

As you can tell I'm going a bit spare, all I want is wrapper types and not primitives! I guess Scala is trying to optimize the array for iteration, but all I need is a tiny array I can pass to a var args taking arrays of mixed type.. Or maybe a better way of creating that vararg method?? Right now its type is:

Array[Any]*

========

Okay, so I got my original question resolved (though there's still a dispute in the comments over whether I correctly understood why the error was being caused). However, it didn't solve my problem, which is the following:

I am trying to transpose an array of arrays with different types (some are nested, but ultimately either Double or Ints) and can't figure out how to get the scala type system to do it. Here's the basic example:

val integerArray = Array(7, 1, 4)
val nestedIntegerArray = Array(
  Array(6, 10, 60),
  Array(5, 89, 57),
  Array(15, 3, 5)
)
val nestedDoubleArray = Array(
  Array(.13, .9, .8),
  Array(.2, .777, .57),
  Array(.15, .3, .5)
)

val brokenComputation = typeErrorExample(integerArray, nestedIntegerArray, nestedDoubleArray)

where the method being called is:

  private def typeErrorExample(arrays: Array[_ <: Any]*)={
    val arrayDataFrame = Array(arrays).transpose
  }

This gives the error:

No implicit view available from Seq[Array[_]] => Array[U].
[INFO]     val arrayDataFrame = Array(arrays).transpose

What's the right way to do this? Should I use a non-array data structure instead?

Hopefully having more of the code will help the experts understand what was causing my original error too. (inputIds was renamed to integerArray for consistency).

11
  • 2
    val inputIds: Array[Any] = Array[Any](7, 1, 4) works fine for me. Are you sure that's what you tried? Commented Apr 9, 2018 at 22:21
  • @JoePallas Yes, I copy-pasted the code. Just tried actually compiling it and it gives this error: [INFO] found : Array[Array[Int]] [INFO] required: Array[Any] [INFO] Note: Array[Int] <: Any, but class Array is invariant in type T. [INFO] You may wish to investigate a wildcard type such as _ <: Any. (SLS 3.2.10) Commented Apr 9, 2018 at 22:24
  • The question is confused. A method that takes Array[Any] as an argument will accept an Array[Int] without error. Commented Apr 9, 2018 at 22:34
  • @jwvh Updated my question with a larger code snippet which might clarify something for you. Commented Apr 9, 2018 at 22:45
  • Post a minimal reproducible example of Array[Any](7, 1, 4) failing, if it fails. Commented Apr 9, 2018 at 22:51

5 Answers 5

1

Explicitly casting is not ideal, but should do the trick.

 Array(1, 2, 3).asInstanceOf[Array[Any]]
Sign up to request clarification or add additional context in comments.

Comments

1

@Adair, this appears to arrange the input arrays into the Array[Array[Any]] type that you're looking for.

def transpose(arrays: Array[_]*) :Array[Array[_]] =
  arrays.indices.map(x => arrays.map(_(x)).toArray).toArray

Now here's a word of advice: DON'T DO IT.

  • It's unsafe. This will throw an exception if the number of arrays passed exceeds the dimensions of each array.
  • Type Any is not what you want. When a data element becomes type Any then you've lost type information and it's a royal pain trying to get it back.

Comments

0

Jason's answer works, so does doing what the compiler error suggests when I ran it outside of intelliJ and changing the datatype in the method signature:

arrays: Array[_ <: Any]*

Comments

0
scala> Array(1, 2, 3).map(_.asInstanceOf[Any])
res6: Array[Any] = Array(1, 2, 3)

Comments

0

There are many answers which are correct. But why even need to do asInstanceOf. You can do it easily.

scala> val arr: Array[Any] = Array(1,2,3)
arr: Array[Any] = Array(1, 2, 3)

Simple, Work is done.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.