gpt4 book ai didi

kotlin - Kotlin中实际关键字的目的是什么

转载 作者:行者123 更新时间:2023-12-04 18:57:33 27 4
gpt4 key购买 nike

我注意到协程的一些函数标有 actual关键词。

来自 documentation :

actual denotes a platform-specific implementation in multiplatform projects



据我了解 documentation actual关键字用于多平台项目,应与 expect 配对使用关键词。

像这样的东西:

常用模块:
package org.jetbrains.foo

expect class Foo(bar: String) {
fun frob()
}

fun main(args: Array<String>) {
Foo("Hello").frob()
}

对应模块:
package org.jetbrains.foo

actual class Foo actual constructor(val bar: String) {
actual fun frob() {
println("Frobbing the $bar")
}
}

这个案子很清楚。

但在包装 kotlinx.coroutines.experimental我注意到有些函数像 launchwithContext标记为 actual但是没有 expect包中的功能。

那么 actual的目的是什么?关键字 没有 expect ?

最佳答案

kotlinx.coroutines 库实际上利用了多平台项目,因为它同时支持 JVM 和 JS 编译目标。

可以找到常用模块here ,以及具体的 expect您提到的函数的声明here .

关于kotlin - Kotlin中实际关键字的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48794879/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com