Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin泛型:无法使用星号参数按类型排序_Kotlin_Generics - Fatal编程技术网

Kotlin泛型:无法使用星号参数按类型排序

Kotlin泛型:无法使用星号参数按类型排序,kotlin,generics,Kotlin,Generics,我试图对对象列表进行排序——让我们根据它们的值调用它们StockRows,它们都实现了 接口详细信息StockCell:可比 例如,此类表示StockRow中的值: data class SharesCell(val shares: Int?) : DetailedStockCell<SharesCell> { override fun compareTo(other: SharesCell): Int { return this.shares.compare

我试图对对象列表进行排序——让我们根据它们的值调用它们
StockRow
s,它们都实现了

接口详细信息StockCell:可比

例如,此类表示
StockRow
中的值:

data class SharesCell(val shares: Int?) : DetailedStockCell<SharesCell> {
    override fun compareTo(other: SharesCell): Int {
        return this.shares.compareTo(other.shares)
    }
}
但是,当我试图通过
columnIndex
map按所选值对
StockRows
列表进行排序时,
sortBy{}
无法推断排序的类型

val masterList: List<StockRow> = //whatever list

fun sortStocksBy(selectedColumn: Int) : List<StockRow>{
    return masterList.sortedBy { stockRow -> stockRow.columnIndex[selectedColumn] }
}

我觉得我在这里错过了一些简单的东西,所以非常感谢任何明智的帮助

你不能像这样把排序和泛型结合起来。排序需要知道排序依据的类型,以便在运行时可以知道方法签名,但由于类型擦除,泛型在运行时不可能做到这一点

DetailedStockCell
的存在并不能真正完成任何事情
sortedBy
想要与它自己的类型类似的东西,但是作为
DetailedStockCell
的东西不能保证编译器可以做到这一点。它可以与实现它的其他类进行比较,但不能与同一类的实例进行比较。我只需删除该接口,并让每个单元格类型实现可比较的

因此,您需要具体地对每个类型及其具体类型进行排序。您可以删除
columnIndex
映射并创建如下函数:

fun Iterable<StockRow>.sortedByColumn(columnIndex: Int): List<StockRow> {
    return when (columnIndex) {
        0 -> sortedBy(StockRow::symbolCell)
        1 -> sortedBy(StockRow::sharesCell)
        2 -> sortedBy(StockRow::priceCell)
        3 -> sortedBy(StockRow::totalGainCell)
        4 -> sortedBy(StockRow::percentOfPortfolioCell)
        else -> error("Nonexistant column: $columnIndex")
    }
}
funiterable.sortedByColumn(columnIndex:Int):列表{
返回时间(列索引){
0->sortedBy(StockRow::symbolCell)
1->sortedBy(StockRow::sharesCell)
2->sortedBy(StockRow::priceCell)
3->按(StockRow::totalGainCell)分类
4->按(库存行::PortfolioCell百分比)分类
else->错误(“不存在的列:$columnIndex”)
}
}

谢谢@Tenfour04,你的回答很好地解释了为什么我走错了方向。这也是泛型局限性的一个很好的例子,有时你很难理解它。
Type parameter bound for R in inline fun <T, R : Comparable<R>> Iterable<T>.sortedBy(crossinline selector: (T) -> R?): List<T>
 is not satisfied: inferred type DetailedStockCell<*> is not a subtype of Comparable<DetailedStockCell<*>>
interface DetailedStockCell <out T> : Comparable<T>

data class DetailedStockRow(...){
    val columnIndex: Map<out Int, DetailedStockCell<Any>> = mapOf(...)
}
fun Iterable<StockRow>.sortedByColumn(columnIndex: Int): List<StockRow> {
    return when (columnIndex) {
        0 -> sortedBy(StockRow::symbolCell)
        1 -> sortedBy(StockRow::sharesCell)
        2 -> sortedBy(StockRow::priceCell)
        3 -> sortedBy(StockRow::totalGainCell)
        4 -> sortedBy(StockRow::percentOfPortfolioCell)
        else -> error("Nonexistant column: $columnIndex")
    }
}