Kotlin-jvm 和 Kotlin-js 之间 KClass 的不同行为
Different behavior of KClass between Kotlin-jvm and Kotlin-js
我写了一些 kotlin 代码来显示在 jvm 和 js 中执行的行为差异。我该如何解决这个问题?
这个等式:booleanKClass == genericKclass
对于 JVM 是正确的
但是 false 对于 JS
我将粘贴代码,然后是控制台生成的输出(一个用于 jvm,一个用于 js)
如果您从多平台项目中调用 test1(),您会像我一样看到它。
我正在使用 kotlin_version = ‘1.2.51’
fun test1() {
val values = PropertyDelegate()
val result = Result(values)
println("This will call the delegate getter.")
println("result.success is not really important but: ${result.success}")
println("This will call the delegate setter...")
result.success = true
println("end")
}
class Result(del: PropertyDelegate) {
var success: Boolean by del
}
class PropertyDelegate() {
inline operator fun <reified T> getValue(thisRef: Any?, property: KProperty<*>): T {
val booleanKClass = Boolean::class
val genericKclass = T::class
println("getValue (booleanKClass == genericKclass) is ${booleanKClass == genericKclass}")
return true as T
}
inline operator fun <reified T> setValue(thisRef: Any?, property: KProperty<*>, value: T) {
val booleanKClass = Boolean::class
val genericKclass = T::class
println("setValue (booleanKClass == genericKclass) is ${booleanKClass == genericKclass}")
}
}
JVM 输出:
This will call the delegate getter.
getValue (booleanKClass == genericKclass) is true
result.success is true
This will call the delegate setter...
setValue (booleanKClass == genericKclass) is true
end
JS 输出:
This will call the delegate getter.
getValue (booleanKClass == genericKclass) is false
result.success is not really important but: true
This will call the delegate setter...
setValue (booleanKClass == genericKclass) is false
end
自 Kotlin 1.3.41 起它按预期工作。
打印:
This will call the delegate getter.
getValue (booleanKClass == genericKclass) is true
result.success is not really important but: true
This will call the delegate setter...
setValue (booleanKClass == genericKclass) is true
end
我写了一些 kotlin 代码来显示在 jvm 和 js 中执行的行为差异。我该如何解决这个问题?
这个等式:booleanKClass == genericKclass 对于 JVM 是正确的 但是 false 对于 JS
我将粘贴代码,然后是控制台生成的输出(一个用于 jvm,一个用于 js) 如果您从多平台项目中调用 test1(),您会像我一样看到它。 我正在使用 kotlin_version = ‘1.2.51’
fun test1() {
val values = PropertyDelegate()
val result = Result(values)
println("This will call the delegate getter.")
println("result.success is not really important but: ${result.success}")
println("This will call the delegate setter...")
result.success = true
println("end")
}
class Result(del: PropertyDelegate) {
var success: Boolean by del
}
class PropertyDelegate() {
inline operator fun <reified T> getValue(thisRef: Any?, property: KProperty<*>): T {
val booleanKClass = Boolean::class
val genericKclass = T::class
println("getValue (booleanKClass == genericKclass) is ${booleanKClass == genericKclass}")
return true as T
}
inline operator fun <reified T> setValue(thisRef: Any?, property: KProperty<*>, value: T) {
val booleanKClass = Boolean::class
val genericKclass = T::class
println("setValue (booleanKClass == genericKclass) is ${booleanKClass == genericKclass}")
}
}
JVM 输出:
This will call the delegate getter.
getValue (booleanKClass == genericKclass) is true
result.success is true
This will call the delegate setter...
setValue (booleanKClass == genericKclass) is true
end
JS 输出:
This will call the delegate getter.
getValue (booleanKClass == genericKclass) is false
result.success is not really important but: true
This will call the delegate setter...
setValue (booleanKClass == genericKclass) is false
end
自 Kotlin 1.3.41 起它按预期工作。
打印:
This will call the delegate getter.
getValue (booleanKClass == genericKclass) is true
result.success is not really important but: true
This will call the delegate setter...
setValue (booleanKClass == genericKclass) is true
end