-
Notifications
You must be signed in to change notification settings - Fork 5
Kotlin 相关问题
坏黑 edited this page May 28, 2022
·
1 revision
因为 Kotlin Script 编译或运行的特殊性,部分 Kotlin 特性在 Artifex 下不再适用,在本篇逐个列举。
Back-end (JVM) Internal error: Failed to generate expression: KtNameReferenceExpression
由于 Kotlin Script 的编译限制,所有脚本均为封闭对象。子类不可访问脚本基类的任何成员,形如:
val testMap = hashMapOf<String, String>()
object TestObject {
init {
testMap["bar"] = "foo" // Back-end (JVM) Internal error
}
}
创建共享资源可参考以下写法:
object OpenAPI {
val testMap = hashMapOf<String, String>()
}
object TestObject {
init {
OpenAPI.testMap["bar"] = "foo"
}
}
[Artifex] > 运行结果异常: (详细信息已发送到控制台)
[Artifex] > java.lang.NoSuchMethodError: kotlin.LazyKt.lazy(Lkotlin1510/jvm/functions/Function0;)Lkotlin/Lazy;
由于 Artifex 的编译机制限制,Lazy 容器无法使用,形如:
val testValue by lazy { 10 }
info(testValue) // java.lang.NoSuchMethodError
由 Artifex 提供的懒加载容器作为替代:
val testValue = LazyGetter { 10 }
info(testValue()) // 注意这里是运行了 invoke() 方法