3
var channelsNumber = track.getNumberOfChannels()
var framesNumber = lastFrame - firstFrame
var frames = Array.ofDim[Int](channelsNumber)(framesNumber)
System.out.println(frames.length);
System.out.println(frames.length);

I try to define two dimensional array of integers. And I get this error:

[error] .../test.scala:58: type mismatch;
[error]  found   : Int
[error]  required: scala.reflect.ClassManifest[Int]
[error]     var frames = Array.ofDim[Int](channelsNumber)(framesNumber)
[error]                                                   ^
[error] one error found

What is "scala.reflect.ClassManifest[Int]"? Why channelsNumber passes and framesNumber, which is also an integer doesn't?

2 Answers 2

5

Now, you are calling method ofDim [T] (n1: Int)(implicit arg0: ClassManifest[T]) which you don't want to. Change the call to Array.ofDim[Int](channelsNumber,framesNumber) and the method ofDim [T] (n1: Int, n2: Int)(implicit arg0: ClassManifest[T]) will be called. You want to leave the implicit parameter group implicit.

And - class manifest is a way how to preserve type information in generic classes.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! What is strange, it was working properly with Array.ofDim[Int](4)(4), but it didn't with with variable.
I get the same error type mismatch when I run the code Array.ofDim[Int](4)(4) in the repl...
4

First your error: ofDim takes all the dimension in a single parameter list. You need

Array.ofDim[Int](channelsNumber, framesNumber)

Second, ClassManifest. Due to type erasure, and the fact than in the JVM, arrays are very much like generics, but are not generic (among other things, no type erasure), the generic method ofDim needs to be passed the type of the elements. That is the ClassManifest, which is close to passing a Class in java (you have to do the same in java -or pass an empty array of the proper type, in Collection.toArray- if you have a generic method that must return an array) This comes as as an implicit arguments, that is there is another parameter list with this argument, but the scala compiler will try to fill it automatically, without you having to write it in the code. But if you give a second parameter list, it means you intend to pass the ClassManifest yourself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.