Why it isn't possible to use `*doubleArrayOf(1.0, ...
# announcements
l
Why it isn't possible to use
*doubleArrayOf(1.0, 2.0)
?
s
it is though?
s
It should be possible, if you assign it to a
vararg
.
s
though this is usually a code smell if you’re using this at the call site of a vararg function
l
I just simplified
I have a DoubleArray and a function that takes
vararg Double
I can't pass the DoubleArray to it
r
Works fine for me
Copy code
fun test2() {
    val nums = doubleArrayOf(1.0, 2.0)
    test1(*nums)
    test1(*doubleArrayOf(1.0, 2.0))
}

fun test1(vararg nums: Double) {
    println(nums.contentToString())
}
s
there must be something else we’re missing because I can definitely do what you’ve described
Copy code
fun foo(vararg doubles: Double) {

}

fun bar(doubleArray: DoubleArray) {
  foo(*doubleArray)
}
l
Maybe this is the new IntelliJ compile testing?
1
I remember there's a compiler inference of some sort that was enabled only in IntelliJ, but not in gradle
I'll double check what I'm doing one more time. A dummy attempt worked:
image.png
Perhaps it's because it's a Java vararg function?
s
r
Is the java constructor
Double...
or
double...
? The first will take
Array<Double>
, the second
DoubleArray
.
l
Java is T..t
But specifically with double it's not working
Works for int, string...
It's to be used in hamcrrst Matchers.hasItems(*myDoubles)
r
It works for
double...
for me, but
Double...
requires
*arrayOf(1.0, 2.0, ...)
s
Double...
yikes
r
@LeoColman That's a generic function, so it will require the object type, not the primitive (i.e.
Double...
instead of
double...
, and thus
*arrayOf(...)
instead of
*doubleArrayOf(...)
).