# Kotin Koan

**URL:** <https://discuss.kotlinlang.org/t/kotin-koan/25638>\
**Category:** Support\
**Created:** [September 11, 2022, 4:07pm UTC](https://discuss.kotlinlang.org/t/kotin-koan/25638 "2022-09-11T16:07:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaulsalil88](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kaulsalil88/32/8414_2.png) [@kaulsalil88](https://discuss.kotlinlang.org/u/kaulsalil88)\
**Post date:** [September 11, 2022, 4:07pm UTC](https://discuss.kotlinlang.org/t/kotin-koan/25638/1 "2022-09-11T16:07:32Z")

</div>

I was trying to solve the null safety question on kotlin koans ([Kotlin Playground: Edit, Run, Share Kotlin Code Online](https://play.kotlinlang.org/koans/Introduction/Nullable%20types/Task.kt)). I wrote the following code fun

```auto
sendMessageToClient(
        client: Client?, message: String?, mailer: Mailer
) {
		
    message?.let{
        m-> {
            client?.let{ c ->
                if(c.personalInfo != null && c.personalInfo.email != null ){
                    mailer.sendMessage(c.personalInfo.email,m)
                }
                
            }
        }
    }
}

```

And I get the following error  
Fail: everythingIsOk: The function ‘sendMessage’ should be invoked expected: but was:

Passed: noClient

Passed: noMessage

Passed: noPersonalInfo

Passed: noEmail What does it mean ?

---

<div class="post-metadata">

**Author:** ![tlin47](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@tlin47](https://discuss.kotlinlang.org/u/tlin47)\
**Post date:** [September 12, 2022, 12:34am UTC](https://discuss.kotlinlang.org/t/kotin-koan/25638/2 "2022-09-12T00:34:15Z")

</div>

Welcome to the Kotlin community. 🖖

Now to your question: It means that your code does not call `sendMessage` even when all parameters are ok.

I think, the reason for that is that the number of braces (curly brackets) is wrong. `let` computes the given operation and returns its result. I guess in your version with the additional braces, the result is a full function of sending message instead of just sending message - basically the difference between `doSomething()` and `{ doSomething() }`.  
_FunFact: That means, if you add “`?.invoke()`” after the last closing brace, it actually passes all tests._

However, I am not an expert on `let`, but the point of the exercise is to use Kotlin operations to simplify the given Java code and your solution is actually way more complicated than the original: it is not really shorter than the Java version and it is nested in three levels - which invites easy-to-make-but-difficult-to-see-mistakes like “too many braces”.

I guess the idea is using safe calls in chains like `if (client?.personalInfo?.email != null)`. With that you can reduce it to a simple two-line solution with only one level.

---

<div class="post-metadata">

**Author:** ![kaulsalil88](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kaulsalil88/32/8414_2.png) [@kaulsalil88](https://discuss.kotlinlang.org/u/kaulsalil88)\
**Post date:** [September 15, 2022, 9:03am UTC](https://discuss.kotlinlang.org/t/kotin-koan/25638/3 "2022-09-15T09:03:43Z")

</div>

Yup thnx for the input I didn’t use idiomatic kotlin . The issue was caused by the curly braces adding a run after m → solved everything .

```auto
fun sendMessageToClient(
        client: Client?, message: String?, mailer: Mailer
) {
		
    message?.let{
        m-> run{
            client?.let{ c ->
                if(c.personalInfo != null && c.personalInfo.email != null ){
                    mailer.sendMessage(c.personalInfo.email,m)
                }
                
            }
        }
    }
}

class Client(val personalInfo: PersonalInfo?)
class PersonalInfo(val email: String?)
interface Mailer {
    fun sendMessage(email: String, message: String)
}

```

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [September 15, 2022, 11:05am UTC](https://discuss.kotlinlang.org/t/kotin-koan/25638/4 "2022-09-15T11:05:45Z")

</div>

Re. the braces: one potential source of confusion in Kotlin is that, unlike most other languages, braces are used for two different things in Kotlin:

- **Group statements into a block.** This is what they mean in C, Java, and a host of other languages. It provides a scope for local variables. And it delimits the body of a class, interface, or function, and a way to execute multiple statements in a `for` or `while` loop, a `try` block, or an `if` branch.

In Kotlin, **braces define a block _only_ in the specific cases above**. In all other cases they:

- **Define a lambda.** This is an anonymous function or closure, which is not run immediately but can be stored and/or passed as a parameter. In many other languages, you need to provide an arrow (`->` or `=>`) to define a lambda, but Kotlin lets you omit that if the lambda takes no parameters, or takes one (which can be referred to as `it`), and so the braces are required.

What makes this confusing is that, although braces define a block only in specific cases, those cases are extremely common (and work just like many other languages), and so it’s easy to miss the fact that in Kotlin, **arbitrary braces define a lambda**. You can’t use them to define a block just anywhere; instead, you have to use e.g. [`run{ … }`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/run.html) (in which the braces define a lambda).

* * *

In the question, this applies to the line `m -> {`, which defines a lambda. That lambda will become the result of the overall `message?.let{` expression — but since the lambda is never _called_, none of the statements inside it will get executed.

If you remove that brace (and the corresponding close brace), then you might find it works as intended. (If there were multiple statements inside, you could instead add `run` before the open brace.)

---

<div class="post-metadata">

**Author:** ![kaulsalil88](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/kaulsalil88/32/8414_2.png) [@kaulsalil88](https://discuss.kotlinlang.org/u/kaulsalil88)\
**Post date:** [September 15, 2022, 11:16am UTC](https://discuss.kotlinlang.org/t/kotin-koan/25638/5 "2022-09-15T11:16:58Z")

</div>

Thanks so much for giving such a in depth reply and to clear my doubt . The following works

```auto
fun sendMessageToClient(
        client: Client?, message: String?, mailer: Mailer
) {
		
    message?.let{
        m-> run{
            client?.let{ c ->
                if(c.personalInfo != null && c.personalInfo.email != null ){
                    mailer.sendMessage(c.personalInfo.email,m)
                }
                
            }
        }
    }
}

class Client(val personalInfo: PersonalInfo?)
class PersonalInfo(val email: String?)
interface Mailer {
    fun sendMessage(email: String, message: String)
}

```

---

<div class="post-metadata">

**Author:** ![tlin47](https://avatars.discourse-cdn.com/v4/letter/t/bc79bd/32.png) [@tlin47](https://discuss.kotlinlang.org/u/tlin47)\
**Post date:** [September 15, 2022, 5:54pm UTC](https://discuss.kotlinlang.org/t/kotin-koan/25638/6 "2022-09-15T17:54:23Z")

</div>

Just removing the additional braces would work, too:

```auto
fun sendMessageToClient(
        client: Client?, message: String?, mailer: Mailer
) {
    message?.let { m ->
        client?.let { c ->
            if (c.personalInfo != null && c.personalInfo.email != null) {
                mailer.sendMessage(c.personalInfo.email,m)
            }
        }
    }
}

```

or keeping the braces and calling invoke on the result (the solution above is better, just mention it for fun):

```auto
fun sendMessageToClient(
        client: Client?, message: String?, mailer: Mailer
) {
    message?.let { m -> {
            client?.let { c ->
                if (c.personalInfo != null && c.personalInfo.email != null) {
                    mailer.sendMessage(c.personalInfo.email,m)
                }
            }
    	}
    }?.invoke()
}

```

or using optional chaining:

```auto
fun sendMessageToClient(
        client: Client?, message: String?, mailer: Mailer
) {
    if (message != null && client?.personalInfo?.email != null) {
        mailer.sendMessage(client.personalInfo.email, message)
    }
}

```

or using a generic function that adds the optional chaining idea to function parameters:

```auto
fun sendMessageToClient(
        client: Client?, message: String?, mailer: Mailer
) {
    ifNotNull(mailer::sendMessage, client?.personalInfo?.email, message)
}

fun <S, T, U> ifNotNull(action: (S, T) -> U, arg1: S?, arg2: T?): U? {
    if (arg1 == null || arg2 == null) {
        return null
    }
    return action(arg1, arg2)
}

```

Kotlin is fun. 🖖
