Jonathan Olsson
11/11/2022, 3:17 PMlet
function always been callable on nullable types (ie not <T : Any>
)? I could have sworn that the following didn't use to work:
fun foo(s: String?) {
s.let { } // <- expected I had to do s?.let { }
}
Has it always worked like this or was this changed at some point?Jonathan Olsson
11/11/2022, 3:21 PMKevin Healy
11/11/2022, 3:22 PMJonathan Olsson
11/11/2022, 3:25 PMKevin Del Castillo
11/11/2022, 3:48 PM/**
* Calls the specified function [block] with `this` value as its argument and returns its result.
*
* For detailed usage information see the documentation for [scope functions](<https://kotlinlang.org/docs/reference/scope-functions.html#let>).
*/
@kotlin.internal.InlineOnly
public inline fun <T, R> T.let(block: (T) -> R): R {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return block(this)
}
The receiver of let
is an unbounded T
not T : Any
, that's why it also works with nullable types