推断的类型是可空值的集合,但预期是不可空值的集合

Inferred type is Collection of nullables but expected Collection of non-nullables

我打算处理一些数据(它是从 json 对象 Nuu 解析的列表,在下面的示例中我只是对列表进行了存根)。为了处理该集合,我将消费者 println 传递到方法 getAllNuu 中,该方法检索并解析 "json".

不过,我从 json 收到的数据包含重复项(基于三个字段的两个 [Nuu.loNuu.la],请参阅覆盖 Nuu.equals -生成),所以我想通过使用扩展函数 Nuu.merge

将它们合并在一起

我没有看到任何错误,直到我尝试编译下面的代码,然后我得到: Error:(7, 5) Kotlin: Type mismatch: inferred type is Collection<Nuu?> but Collection<Nuu> was expected

为什么 kotlin 会推断出一个包含可为空对象的集合?它发生在哪里?

我也很感激任何使代码更好或更像 kotlin 的建议。

我的环境:kotlinc-jvm 1.3.70 (JRE 12.0.1+12)

代码:

    fun main(args: Array<String>) {
        getAllNuu { data -> println(mergeNuus(data)) }
    }

    fun mergeNuus(data: Collection<Nuu>): Collection<Nuu> =
        // grouping two same (based on their equals) elements
        data.groupingBy { it }
     // ^ Error:(7, 5) Kotlin: Type mismatch: inferred type is Collection<Nuu?> but Collection<Nuu> was expected
            // merging the equal elemnts into one
            .aggregate { _, accumulator: Nuu?, element: Nuu, first ->
                if (first)
                    element
                else
                    accumulator!!.merge(element)
            }.values

    // supposedly this is a http request which processes
    // the retrieved data by passed in consumer 'printout'
    fun getAllNuu(printout: (data: Collection<Nuu>) -> Unit) {
        val nuuList = listOf(
            Nuu(3, "t", 1),
            Nuu(5, "6", 2),     // say this is a http request
            Nuu(7, "a", 3),     // just been stubbed with the list
            Nuu(3, "a", 4),
            Nuu(5, "5", 5),
            Nuu(2, "2", 6),
            Nuu(3, "t", 7),
            Nuu(1, "1", 8),
            Nuu(5, "5", 9),
            Nuu(2, "2", 10)
        )
        // processing the data with consumer passed
        // from the main method
        printout.invoke(nuuList)
    }

    data class Nuu(
        val lo: Int,
        val la: String,
        val su: Int
    ) {
        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (javaClass != other?.javaClass) return false
            other as Nuu
            if (lo != other.lo) return false
            if (la != other.la) return false
            return true
        }

        override fun hashCode(): Int {
            var result = lo
            result = 31 * result + la.hashCode()
            return result
        }
    }
    // similarity based on equals, and sumup the field 'su'
    fun Nuu.merge(other: Nuu) = Nuu(lo, la, su + other.su)

谢谢

除了我在评论 (.aggregate<Nuu, Nuu, Nuu>) 中给出的解决方法外,这也可以在不指定任何其他类型的情况下使用:

    data.groupingBy { it }
        // merging the equal elemnts into one
        .aggregate { _, accumulator: Nuu?, element: Nuu, first ->
            val res = 
                if (first)
                    element
                else
                    accumulator!!.merge(element)
            res
        }.values

我真的看不出有什么理由让你的代码在编译时不编译,对我来说这似乎是一个编译器错误。当然,我也不明白为什么这里会出现错误,你没有做任何棘手或不寻常的事情。