Ofir Bar
02/18/2020, 7:07 PMT
to 2 different objects?
For example, here we limit to type Cat
or Dog
something like(this is pseudocode):
private fun<T : Cat or Dog> convertToIntArray(objects: List<T>) : IntArray {
val objectIds = arrayListOf<Int>()
objects.forEach {
objectIds.add(it.id)
}
return objectIds.toIntArray()
}
// Then pass something like this:
val cats = convertToIntArray(oldListOfCats)
val dogs = convertToIntArray(oldListOfDogs)
KamilH
02/18/2020, 7:14 PMCat
and Dog
extend or implement some class/interface (Animal
for example) and then you could use where
something like that:
private fun<T> convertToIntArray(objects: List<T>) : IntArray where T : Animal {
val objectIds = arrayListOf<Int>()
objects.forEach {
objectIds.add(it.id)
}
return objectIds.toIntArray()
}
https://kotlinlang.org/docs/reference/generics.htmlOfir Bar
02/18/2020, 7:15 PMBrianZhang
02/21/2020, 4:14 PM