1

I am trying to use the library dcm4che3 in Scala

My Scala code is

val item: Attributes = new Attributes
val x: Int = ints.toArray.length
val vr: VR = VR.OW
val intArray: Array[Int] = ints.toArray
item.setInt(Tag.LUTData, vr, intArray)

and I get an error

Error:(125, 10) overloaded method value setInt with alternatives:
  (x$1: String,x$2: Int,x$3: org.dcm4che3.data.VR,x$4: Int*)Object <and>
  (x$1: Int,x$2: org.dcm4che3.data.VR,x$3: Int*)Object
 cannot be applied to (Int, org.dcm4che3.data.VR, Array[Int])
    item.setInt(Tag.LUTData, vr, intArray)

I notice in the error it is asking for an Int*. The java signature is

setInt(int,org.dcm4che3.data.VR,int[])

I understand that Array[Int] is the scala equivalent of int[]. What is an Int*? Why doesn't this work?

Codey McCodeface
  • 2,988
  • 6
  • 30
  • 55

1 Answers1

3

Int* is a varargs parameter, the equivalent of Java's int.... Just like Java, you can pass an array or an arbitrary number of arguments, but to avoid the ambiguity you get in Java when you pass null, there's a special syntax in Scala. Call the function as follows:-

item.setInt(Tag.LUTData, vr, intArray:_*)

In fact, you probably don't need an array at all: you can pass any sequence to a varargs function in Scala, not just an array, so if your ints variable is some kind of sequence, you can pass it in place of intArray, and remove intArray completely.

See also What does `:_*` (colon underscore star) do in Scala?, which is the reverse of your question.

Community
  • 1
  • 1
Dan Hulme
  • 14,779
  • 3
  • 46
  • 95