按类型分类的 Kotlin 扩展函数

Kotlin extension functions by type

根据类型向 class 添加函数的惯用方法是什么。下面的例子使用List作为class,类型参数<T>是列表内部对象的class。假设您要根据这些列表的类型使用不同的比较器对每个列表进行排序。

data class A(val foo: String)
data class B(val bar: Int)

private val aComparator: Comparator<A> = Comparator { lhs, rhs -> rhs.foo.compareTo(lhs.foo) }
private val bComparator: Comparator<B> = Comparator { lhs, rhs -> rhs.bar.compareTo(lhs.bar) }

fun <T: A> List<T>.sort(): List<T> {
    return this.sortedWith<T>(aComparator)
}

fun <T: B> List<T>.sort(): List<T> {
    return this.sortedWith<T>(bComparator)
}

这给出了一个错误,指出由于 Java 的重载规则,两个排序函数具有相同的签名。在 Java 中,我可能会给它们两个不同的可识别名称,但作为 Kotlin 扩展(例如 a.sortA() b.sortB()),它相当丑陋。 Kotlin 不会向 List< B > 显示 sortA,因此似乎有更好的方法来编写 sort() 来处理不同对象上的不同比较器。

该示例非常简单,但想象一下,如果我无权修改 class A 和 B,那么我将无法使用继承或在它们上实现接口。我还考虑过为每个 class 添加一个比较器并使用 Any?但这似乎也很麻烦。

一个答案似乎是:

@JvmName("sortA")
fun <T: A> List<T>.sort(): List<T> {
    return this.sortedWith<T>(aComparator)
}

@JvmName("sortB")
fun <T: B> List<T>.sort(): List<T> {
    return this.sortedWith<T>(bComparator)
}

这似乎解决了 Java 的通用类型擦除问题。

在此处找到:https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html

On this site, I found this solution

而不是这个

fun Iterable<Long>.average(): Double {}
fun Iterable<Int>.average(): Double {}

使用平台名称

fun Iterable<Long>.average(): Long {
}
platformName("averageOfInt") fun Iterable<Int>.average(): Int {
}

编辑:这已被弃用,请改用 JvmName。