Reflection Kotlin jvm和Kotlin js之间KClass的不同行为
我编写了一些kotlin代码来显示jvm和js上执行的行为差异。我怎样才能解决这个问题 此等式为:booleanKClass==genericKclass JVM是否为真 但JS的为false 我将粘贴代码,然后是控制台生成的输出(一个用于jvm,一个用于js) 如果您从多平台项目调用test1(),您将看到这一点,就像我所做的一样。 我使用的是kotlin_version='1.2.51'Reflection Kotlin jvm和Kotlin js之间KClass的不同行为,reflection,kotlin,kotlin-js,Reflection,Kotlin,Kotlin Js,我编写了一些kotlin代码来显示jvm和js上执行的行为差异。我怎样才能解决这个问题 此等式为:booleanKClass==genericKclass JVM是否为真 但JS的为false 我将粘贴代码,然后是控制台生成的输出(一个用于jvm,一个用于js) 如果您从多平台项目调用test1(),您将看到这一点,就像我所做的一样。 我使用的是kotlin_version='1.2.51' fun test1() { val values = PropertyDelegate()
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}")
}
}
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
有趣。我不明白为什么这不应该起作用,即使在技术上有限制。在JS中,
genericKclass
显示为class null
。请随意投票相关问题JFYI:问题已经解决。
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