Kotlin 自引用对象中的java.lang.ExceptionInInitializeError?
我的jvm编译器有问题 我正在尝试为类编写工厂方法。factory方法有一个init()块,用于帮助定义新对象的行为。当此方法为JVM编译时,我在运行它时遇到了一个问题:Kotlin 自引用对象中的java.lang.ExceptionInInitializeError?,kotlin,jvm,Kotlin,Jvm,我的jvm编译器有问题 我正在尝试为类编写工厂方法。factory方法有一个init()块,用于帮助定义新对象的行为。当此方法为JVM编译时,我在运行它时遇到了一个问题: java.lang.ExceptionInInitializerError Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method type.ProblematicKt.nullable, p
java.lang.ExceptionInInitializerError
Caused by: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method type.ProblematicKt.nullable, parameter $this$nullable
显然,当我试图运行problematicInit()块时,对象还没有定义。我该如何解决这个问题
这似乎是一个JVM问题。它似乎适用于Javascript编译。我的理解是,getProblem将被提升,但范围内的内容将被推迟到设计为稍后运行的运行—在工厂方法完成之后
interface ProblematicBuilderScope {
fun problematicInit(getX: () -> ProblematicInterface)
}
fun getProblematic() = X
class Problematic(...): ProblematicInterface
// Factory method with init() block
val X = Problematic.factory(...) {
problematicInit{ getProblematic() }
}
fun factory(init: ProblematicBuilderScope.() -> Unit): Problematic {
val newObject = Problematic(...)
val scope = ProblematicBuilderScope(newObject)
scope.init()
return newObject
}
这里有一个更简单的方法来实现相同的生成器实现
interface ProblematicInterface
class Problematic(): ProblematicInterface
fun buildProblematic(init: Problematic.() -> Unit): Problematic {
val newObject = Problematic()
init(newObject)
return newObject
}
val x = buildProblematic {
// this object type inside this clouse is Problematic
}
这里有一个更简单的方法来实现相同的生成器实现
interface ProblematicInterface
class Problematic(): ProblematicInterface
fun buildProblematic(init: Problematic.() -> Unit): Problematic {
val newObject = Problematic()
init(newObject)
return newObject
}
val x = buildProblematic {
// this object type inside this clouse is Problematic
}