Kotlin 接收器名称冲突
Kotlin Receiver Name Clash
当 运行 以下代码片段时,编译器正尝试根据 kotlin.collections
中定义的接收器解析方法 isEmpty
,原因我不完全理解。方法 isEmpty
在 java.util.Optional
中定义。如果我将 isEmpty
替换为 !isPresent
,代码将按预期工作。
private fun getThreadCount(param: Optional<String>): Int {
return if(param.isEmpty) {
DEFAULT_THREAD_COUNT
} else {
val threads = param.get()
try {
threads.toInt()
} catch (ex: NumberFormatException) {
throw RuntimeException("Failed to parse threads parameter: $threads")
}
}
}
> Task :compileKotlin FAILED
e: /home/.../TaskParams.kt: (51, 25): Function invocation 'isEmpty(...)' expected
e: /home/.../TaskParams.kt: (51, 25): Unresolved reference. None of the following
candidates is applicable because of receiver type mismatch:
public inline fun <T> Array<out ???>.isEmpty(): Boolean defined in kotlin.collections
public inline fun BooleanArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun ByteArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun CharArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun CharSequence.isEmpty(): Boolean defined in kotlin.text
public inline fun DoubleArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun FloatArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun IntArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun LongArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun ShortArray.isEmpty(): Boolean defined in kotlin.collections
这是针对 Kotlin 1.3.72 编译的
java.util.Optional
isEmpty()
was added in Java 11. The Android version of Optional
(或者你的目标是什么)没有。
只需使用 !isPresent()
,或使用 kotlin 语法糖 !isPresent
。
当 运行 以下代码片段时,编译器正尝试根据 kotlin.collections
中定义的接收器解析方法 isEmpty
,原因我不完全理解。方法 isEmpty
在 java.util.Optional
中定义。如果我将 isEmpty
替换为 !isPresent
,代码将按预期工作。
private fun getThreadCount(param: Optional<String>): Int {
return if(param.isEmpty) {
DEFAULT_THREAD_COUNT
} else {
val threads = param.get()
try {
threads.toInt()
} catch (ex: NumberFormatException) {
throw RuntimeException("Failed to parse threads parameter: $threads")
}
}
}
> Task :compileKotlin FAILED
e: /home/.../TaskParams.kt: (51, 25): Function invocation 'isEmpty(...)' expected
e: /home/.../TaskParams.kt: (51, 25): Unresolved reference. None of the following
candidates is applicable because of receiver type mismatch:
public inline fun <T> Array<out ???>.isEmpty(): Boolean defined in kotlin.collections
public inline fun BooleanArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun ByteArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun CharArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun CharSequence.isEmpty(): Boolean defined in kotlin.text
public inline fun DoubleArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun FloatArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun IntArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun LongArray.isEmpty(): Boolean defined in kotlin.collections
public inline fun ShortArray.isEmpty(): Boolean defined in kotlin.collections
这是针对 Kotlin 1.3.72 编译的
java.util.Optional
isEmpty()
was added in Java 11. The Android version of Optional
(或者你的目标是什么)没有。
只需使用 !isPresent()
,或使用 kotlin 语法糖 !isPresent
。