Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin null and kotlin.Unit confusion in expression body

Could anyone explain the result of following code snippet? Why the first function call returns kotlin.Unit?

kotlin version 1.5.21, java11

fun <T> testMethod(data: String?): T? = data?.let { return null }
println(testMethod<String>(null))  // --> kotlin.Unit

fun <T> testMethod2(data: String?): T? = data?.let { null }
println(testMethod2<String>(null)) // --> null

Why the result is different in kotlin 1.4.30 - https://pl.kotl.in/uFZDE3O9e?

like image 415
ghmulti Avatar asked Nov 07 '22 00:11

ghmulti


1 Answers

It was a compiler issue and already reported here - https://youtrack.jetbrains.com/issue/KT-47527, fixed in 1.6.0-dev-970.

Ref: https://github.com/jetbrains/kotlin/commit/ae608ea67fc589c4472657dc0317e97cb67dd158

like image 150
ghmulti Avatar answered Nov 14 '22 20:11

ghmulti