kotlin - 更新 kotlin 版本时的类型推断和类型不匹配
kotlin - type inference and type mismatch when updating kotlin version
我在尝试理解以下代码中发生的事情时遇到了一些困难:
fun helperMethodNameA(someId: String, rules: RulesObject) {
val content = JsonNodeFactory.instance.arrayNode().apply { // A & B
add(JsonNodeFactory.instance.objectNode().apply {
set("body", JsonNodeFactory.instance.objectNode().apply { // C
set("text", JsonNodeFactory.instance.objectNode().apply { // D
set("value", JsonNodeFactory.instance.textNode(mapper.writeValueAsString(rules))) // E
})
})
})
}
return helperMethodNameB(someId, content.toString())
}
这个项目依赖于另一个设置了 Kotlin v1.3.20 的项目。依赖项目将 Kotlin 版本提升到 v1.3.60。根据以下内容,此位因更新而中断:
A - [ERROR] <pathToFile> [line, position] Type inference failed: inline fun <T> T.apply(block: T.() -> Unit): T
[ERROR] cannot be applied to
[ERROR] receiver: ArrayNode! arguments: (ArrayNode!.() -> ArrayNode!)
B - [ERROR] <pathToFile> [line, position] Type mismatch: inferred type is ArrayNode!.() -> ArrayNode! but ArrayNode!.() -> Unit was expected
C - [ERROR] <pathToFile> [line, position] Type inference failed: Not enough information to infer parameter T in operator fun <T : JsonNode!> set(p0: String!, p1: JsonNode!): T!
[ERROR] Please specify it explicitly.
D - [ERROR] <pathToFile> [line, position] Type inference failed: Not enough information to infer parameter T in operator fun <T : JsonNode!> set(p0: String!, p1: JsonNode!): T!
[ERROR] Please specify it explicitly.
E - [ERROR] <pathToFile> [line, position] Type inference failed: Not enough information to infer parameter T in operator fun <T : JsonNode!> set(p0: String!, p1: JsonNode!): T!
[ERROR] Please specify it explicitly.
我在这里错过了什么?
解决方案是按以下方式指定类型:
fun helperMethodNameA(someId: String, rules: RulesObject) {
val content = JsonNodeFactory.instance.arrayNode().apply {
add(JsonNodeFactory.instance.objectNode().apply {
set<ObjectNode>("body", JsonNodeFactory.instance.objectNode().apply {
set<ObjectNode>("text", JsonNodeFactory.instance.objectNode().apply {
set<TextNode>("value", JsonNodeFactory.instance.textNode(mapper.writeValueAsString(rules)))
})
})
})
}
return helperMethodNameB(someId, content.toString())
}
我在尝试理解以下代码中发生的事情时遇到了一些困难:
fun helperMethodNameA(someId: String, rules: RulesObject) {
val content = JsonNodeFactory.instance.arrayNode().apply { // A & B
add(JsonNodeFactory.instance.objectNode().apply {
set("body", JsonNodeFactory.instance.objectNode().apply { // C
set("text", JsonNodeFactory.instance.objectNode().apply { // D
set("value", JsonNodeFactory.instance.textNode(mapper.writeValueAsString(rules))) // E
})
})
})
}
return helperMethodNameB(someId, content.toString())
}
这个项目依赖于另一个设置了 Kotlin v1.3.20 的项目。依赖项目将 Kotlin 版本提升到 v1.3.60。根据以下内容,此位因更新而中断:
A - [ERROR] <pathToFile> [line, position] Type inference failed: inline fun <T> T.apply(block: T.() -> Unit): T
[ERROR] cannot be applied to
[ERROR] receiver: ArrayNode! arguments: (ArrayNode!.() -> ArrayNode!)
B - [ERROR] <pathToFile> [line, position] Type mismatch: inferred type is ArrayNode!.() -> ArrayNode! but ArrayNode!.() -> Unit was expected
C - [ERROR] <pathToFile> [line, position] Type inference failed: Not enough information to infer parameter T in operator fun <T : JsonNode!> set(p0: String!, p1: JsonNode!): T!
[ERROR] Please specify it explicitly.
D - [ERROR] <pathToFile> [line, position] Type inference failed: Not enough information to infer parameter T in operator fun <T : JsonNode!> set(p0: String!, p1: JsonNode!): T!
[ERROR] Please specify it explicitly.
E - [ERROR] <pathToFile> [line, position] Type inference failed: Not enough information to infer parameter T in operator fun <T : JsonNode!> set(p0: String!, p1: JsonNode!): T!
[ERROR] Please specify it explicitly.
我在这里错过了什么?
解决方案是按以下方式指定类型:
fun helperMethodNameA(someId: String, rules: RulesObject) {
val content = JsonNodeFactory.instance.arrayNode().apply {
add(JsonNodeFactory.instance.objectNode().apply {
set<ObjectNode>("body", JsonNodeFactory.instance.objectNode().apply {
set<ObjectNode>("text", JsonNodeFactory.instance.objectNode().apply {
set<TextNode>("value", JsonNodeFactory.instance.textNode(mapper.writeValueAsString(rules)))
})
})
})
}
return helperMethodNameB(someId, content.toString())
}