首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kotlin-jvm和Kotlin-js之间KClass的不同行为

Kotlin-jvm和Kotlin-js之间KClass的不同行为
EN

Stack Overflow用户
提问于 2018-07-19 09:08:17
回答 1查看 217关注 0票数 2

我编写了一些kotlin代码,以显示jvm上的执行和js中的不同行为。我怎么才能解决这个问题?

这个等式: booleanKClass == genericKclass对于JVM是true,对于JS是false。

我将粘贴代码,然后粘贴控制台生成的输出(一个用于jvm,另一个用于js),如果您从多平台项目调用test1(),您将看到这一点。我使用的是kotlin_version =‘1.2.51’

代码语言:javascript
复制
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输出:

代码语言:javascript
复制
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产出:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-07-08 18:28:17

自Kotlin 1.3.41以来,它就像预期的那样工作。

指纹:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51418597

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档