是否可以延迟初始化 属性 并断言它?
Is it possible to lazily initialize a property and assert it?
如何延迟初始化字段并对其执行断言?
我试过了
val table: Array<FormatInfo> by lazy {
val t = arrayOf(...)
assert(table.size == FORMAT_COUNT, { System.err.println("GLI error: format descriptor list doesn't match number of supported formats") })
t
}
但是它溢出了:
java.lang.WhosebugError
at gli.GliKt$table.invoke(gli.kt:451)
at gli.GliKt$table.invoke(gli.kt)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
at gli.GliKt.getTable(gli.kt)
at gli.GliKt$table.invoke(gli.kt:693)
at gli.GliKt$table.invoke(gli.kt)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
at gli.GliKt.getTable(gli.kt)
at gli.GliKt$table.invoke(gli.kt:693)...
我该如何解决?
堆栈跟踪看起来像是从 lazy {}
lambda 内部调用 属性,这显然使它崩溃。
这应该有效:
val table: Array<FormatInfo> by lazy {
val t = arrayOf(...)
myAssert(t)
t
}
如何延迟初始化字段并对其执行断言?
我试过了
val table: Array<FormatInfo> by lazy {
val t = arrayOf(...)
assert(table.size == FORMAT_COUNT, { System.err.println("GLI error: format descriptor list doesn't match number of supported formats") })
t
}
但是它溢出了:
java.lang.WhosebugError
at gli.GliKt$table.invoke(gli.kt:451)
at gli.GliKt$table.invoke(gli.kt)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
at gli.GliKt.getTable(gli.kt)
at gli.GliKt$table.invoke(gli.kt:693)
at gli.GliKt$table.invoke(gli.kt)
at kotlin.SynchronizedLazyImpl.getValue(Lazy.kt:131)
at gli.GliKt.getTable(gli.kt)
at gli.GliKt$table.invoke(gli.kt:693)...
我该如何解决?
堆栈跟踪看起来像是从 lazy {}
lambda 内部调用 属性,这显然使它崩溃。
这应该有效:
val table: Array<FormatInfo> by lazy {
val t = arrayOf(...)
myAssert(t)
t
}