IntelliJ中的Kotlin Comparator编译错误

时间:2018-06-06 04:55:35

标签: kotlin

使用Kotlin 1.2.41-release并给出List<Pair<Int, Int>>,以下代码在Intellij中生成编译错误,尽管Gradle命令行构建有效。 sortedWith(compareBy({ it.first }, { it.second }))

Cannot choose among the following candidates without completing type inference.
public fun <T> compareBy(vararg selectors: (???) -> Comparable<*>?): kotlin.Comparator<???> defined in kotlin.comparisons
public fun <T> compareBy(vararg selectors: (Pair<Int, Int>) -> Comparable<*>?): kotlin.Comparator<Pair<Int, Int>> defined in kotlin.comparisons

我该如何解决这个问题?

修改

edges
  .map {
      it.either.run {
          val p = this
          val q = it.other(this)
          val min = min(p, q)
          if (min == p) p to q else q to p
      }
  }
  .sortedWith(compareBy({ it.first }, { it.second }))
  .toList()

其中,edgesIterable<Edge>

class Edge(private val v: Int, private val w: Int, val weight: Double) : Comparable<Edge> {
    val either: Int
        get() = v

    fun other(vertex: Int): Int {
        return if (v == vertex) w else v
    }

    override fun compareTo(other: Edge): Int {
        return weight.compareTo(other.weight)
    }

    override fun toString(): String {
        return "Edge(v=$v, w=$w, weight=$weight)"
    }
}

0 个答案:

没有答案