If I include the following Kotlin function in any Kotlin file, it will cause a failure to load that file. I'd like to understand why.
fun optionalUnitReturningOptionalNothing(): Unit? {
fun localOptionalNothing(): Nothing? {
return null
}
return localOptionalNothing()
}
Here is a sample error message:
Error: Unable to initialize main class NullAssignTestKt
Caused by: java.lang.VerifyError: Bad return type
Exception Details:
Location:
NullAssignTestKt.optionalUnitReturningOptionalNothing()Lkotlin/Unit; @3: areturn
Reason:
Type 'java/lang/Void' (current frame, stack[0]) is not assignable to 'kotlin/Unit' (from method signature)
Current Frame:
bci: @3
flags: { }
locals: { }
stack: { 'java/lang/Void' }
Bytecode:
0000000: b800 0bb0
Moving the internal function outside does not fix the problem, however, the following compiles, loads and executes without a problem:
fun optionalNothing(): Nothing? { return null }
fun optionalUnit(): Unit? { return null }
val lambdaOptionalUnitReturningOptionalNothing: () -> Unit? = { optionalNothing() }
fun main() {
optionalNothing() ?: println("optional nothing worked")
optionalUnit() ?: println("optional unit worked")
lambdaOptionalUnitReturningOptionalNothing() ?: println("lambda worked")
}