how to convert list of child type to list of parent type in Kotlin

3702 views generics
-1

I imagine there is a pretty way of doing this, but I just cannot see it. Say I have the class class Bull: Dog. If I have Set<Bull> bulls and a function fun doSomeCool(vararg input:Dog), what should I do with bulls so that I can pass it to doSomeCool? For example the following doesn't work doSomeCool(*bulls.toTypedArray())

answered question

1 Answer

4

Your function should be generic like this:

fun <D: Dog> do(vararg d: D)

posted this

Have an answer?

JD

Please login first before posting an answer.