Mark Elston (131) [Avatar] Offline
On page 133 you state that if the argument to the safe-call operator is null, the call is skipped and null is used as the value instead. I assume that you mean that the value returned by the expression using the safe-call operator is null.

When you, then, introduce let on page 137, you say
The let function makes it easier to deal with a nullable argument that should be passed to a function that expects a non-null parameter.
You make the point that, using let nothing will happen if the object is null.

However, you still need to use the safe-call operator when using let, which automatically does nothing if the object is null.

So I am a bit confused as to what, exactly, let is doing for us here. It seem that we don't really need it since the safe-call operator does all the work.

Dmitry Jemerov (40) [Avatar] Offline
The safe call operator (?.) is a replacement for the "." operator, which is used to invoke a member of an object, called a receiver. For example, if the API you're using looks like "person.sendEmail", and the person can be null, then you can use the safe call operator to handle that case by replacing the call with "person?.sendEmail()".

If the API you're using looks like it is shown in the book ("sendEmailTo(person)"), then there is no possibility to use the safe call operator directly - we aren't invoking a member on any object.

What the "let" function does for us is change any value into a receiver, and therefore allow to use the safe call syntax with it. If we replace the code with
person.let { sendEmailTo(it)
, the new code does have the "." operator, and we therefore can replace it with "?." to handle the case when person is null.
Mark Elston (131) [Avatar] Offline
So the only thing let does is allow us to execute a lambda in the context of some object. It really has nothing to do with nullable objects, specifically. That is handled by the safe-call operator. I think that is what my confusion was. You introduced let as a function for dealing with nullables, and it really isn't.
Dmitry Jemerov (40) [Avatar] Offline
Yes, that's right. We're introducing 'let' in this context because it's the most common use case for this function.