我可能没有很好地解释标题中的问题,但这里有一个例子:
fun main() {
acceptEnum(inferType())
}
fun acceptEnum(value: MyEnum?) {}
fun <R : Enum<R>?> inferType(): R = TODO()
enum class MyEnum {
VALUE
}
inferType()
函数推断其返回类型并将其限制为通用可为空枚举。 acceptEnum()
函数有一个可为空的枚举参数。当我们写下acceptEnum(inferType())
时,一切都很好。但是,如果我们向 acceptEnum()
添加一个参数并再次将 inferType()
传递到那里,则会发生以下情况:
fun main() {
// first inferType() does not compile with an error:
// Type mismatch: inferred type is MyEnum? but MyEnum was expected
acceptEnum(inferType(), inferType())
}
fun acceptEnum(value: MyEnum?, value2: MyEnum?) {}
如果我们添加更多参数,除了最后一个之外的每个
inferType()
调用都会产生此错误。
这是编译器错误还是我做错了什么?
更新
更新