Has the `let` function always been callable on nul...
# getting-started
j
Has the
let
function always been callable on nullable types (ie not
<T : Any>
)? I could have sworn that the following didn't use to work:
Copy code
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?
k
I believe it's always worked like that, but the difference is that without the preceding safe operator the context object will be of a nullable type inside the block, which generally isn't useful
j
Especially when you use it to call some evil java library that spouts out NPEs... 🙈 Must have remembered this completely wrong then. Thanks!
k
Copy code
/**
 * 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