filterIsInstanceTo所在位置是kotlin.collections.filterIsInstanceTo,其相关用法介绍如下。

用法一

fun <reified R, C : MutableCollection<in R>> Array<*>.filterIsInstanceTo(
    destination: C
): C
fun <reified R, C : MutableCollection<in R>> Iterable<*>.filterIsInstanceTo(
    destination: C
): C

将作为指定类型参数 R 实例的所有元素附加到给定的 destination

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
open class Animal(val name: String) {
    override fun toString(): String {
        return name
    }
}
class Dog(name: String): Animal(name)
class Cat(name: String): Animal(name)

val animals: List<Animal> = listOf(Cat("Scratchy"), Dog("Poochie"))
val cats = mutableListOf<Cat>()

println(cats) // []

animals.filterIsInstanceTo<Cat, MutableList<Cat>>(cats)

println(cats) // [Scratchy]
//sampleEnd
}

输出:

[]
[Scratchy]

用法二

fun <C : MutableCollection<in R>, R> Array<*>.filterIsInstanceTo(
    destination: C, 
    klass: Class<R>
): C
fun <C : MutableCollection<in R>, R> Iterable<*>.filterIsInstanceTo(
    destination: C, 
    klass: Class<R>
): C

将作为指定类实例的所有元素附加到给定的 destination

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
open class Animal(val name: String) {
    override fun toString(): String {
        return name
    }
}
class Dog(name: String): Animal(name)
class Cat(name: String): Animal(name)

val animals: List<Animal> = listOf(Cat("Scratchy"), Dog("Poochie"))
val cats = mutableListOf<Cat>()

println(cats) // []

animals.filterIsInstanceTo(cats, Cat::class.java)

println(cats) // [Scratchy]
//sampleEnd
}

输出:

[]
[Scratchy]