javascalabridj

Expression of type Pointer[Float] doesn't conform to expected type Pointer[Float] in Scala


I am new to Scala and I have been trying to use library bridj implemented in Java.
Here is the code (allocateFloats is a method in class org.bridj.Pointer)

import org.bridj.Pointer
import org.bridj.Pointer._

class EstimatingPi {
  def main(args: Array[String]) {
    val n: Int = 1024
    val aPtr : Pointer[Float] = allocateFloats(n)
  }
}

This would result in "Expression of type Pointer[Float] doesn't conform to expected type Pointer[Float]". But if I don't specify the type of the aPtr as shown below, the code compiles.

    val aPtr = allocateFloats(n)

I tried to find the solution online but the questions are mostly like "Expression of type someClass[T1] doesn't conform to expected type someClass[T2]". But in my case, they are the same type.
I would really appreciate any help.


Solution

  • One of them is probably a java.lang.Float. You probably would need to

    val aPtr: Pointer[java.lang.Float] = allocateFloats(n)
    

    If it is not obvious from the allocateFloats documentation, you can find out what type it is by doing something like

    allocateFloats(0).getClass.getName
    

    which will give you the full name with a [L before and ; after, e.g.

    [Ljava.lang.Float;