https://kotlinlang.org logo
a

arekolek

04/06/2020, 10:33 AM
https://pl.kotl.in/lkskQc2rY is there anything the compiler could do to make this work?
Copy code
enum class Foo {
    ONE, TWO
}

fun fooArray(foos: Array<Foo>) {
    println(foos.toList())
}

fun fooVararg(vararg foos: Foo) {
    fooArray(foos) // Type mismatch: inferred type is Array<out Foo> but Array<Foo> was expected
}
in case of final classes (including enums), it's weird that
vararg
should make the
Array
have
out
variance I'm wondering if there's any sense in filing a youtrack issue
d

David Eriksson

04/06/2020, 10:46 AM
Why not change fooArray to take Array<out Foo>?
👍 2
a

arekolek

04/06/2020, 11:42 AM
Why not change fooArray to take Array<out Foo>?
Because
fooArray
is from a dependency I have. But I suggested that to the authors.
I'm just wondering if it even makes sense for the compiler to try to be "smart" in case of final classes.
d

David Eriksson

04/06/2020, 11:57 AM
Just write an extension function to convert the array
a

arekolek

04/06/2020, 1:38 PM
What I did was to just use
arrayOf(*foos)
and it does the job, so that's not a problem
👍 2
2 Views