# Safe call operator chain

**URL:** <https://discuss.kotlinlang.org/t/safe-call-operator-chain/7650>\
**Category:** Uncategorized\
**Created:** [May 2, 2018, 8:43am UTC](https://discuss.kotlinlang.org/t/safe-call-operator-chain/7650 "2018-05-02T08:43:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sodiaan](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@sodiaan](https://discuss.kotlinlang.org/u/sodiaan)\
**Post date:** [May 2, 2018, 8:43am UTC](https://discuss.kotlinlang.org/t/safe-call-operator-chain/7650/1 "2018-05-02T08:43:48Z")

</div>

```
class A(val b: Int)

fun main(args: Array<String>) {
    val b : Int? = null
    val a = if (b != null) { A(b) } else { null }
    println(a?.b.toString().length)
}

```

I thought `a?.b.toString().length` will return null, but instead it does `null.toString().length`  
What are the reasons to work so? Why not return null on first null property?

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [May 2, 2018, 9:37am UTC](https://discuss.kotlinlang.org/t/safe-call-operator-chain/7650/2 "2018-05-02T09:37:02Z")

</div>

[https://kotlinlang.org/docs/reference/null-safety.html#safe-calls](https://kotlinlang.org/docs/reference/null-safety.html#safe-calls)

---

<div class="post-metadata">

**Author:** ![sodiaan](https://avatars.discourse-cdn.com/v4/letter/s/a8b319/32.png) [@sodiaan](https://discuss.kotlinlang.org/u/sodiaan)\
**Post date:** [May 2, 2018, 9:46am UTC](https://discuss.kotlinlang.org/t/safe-call-operator-chain/7650/3 "2018-05-02T09:46:14Z")

</div>

I know about documentation, and there are nothing about reasons why it works so.

---

<div class="post-metadata">

**Author:** ![jstuyts](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jstuyts/32/2141_2.png) [@jstuyts](https://discuss.kotlinlang.org/u/jstuyts)\
**Post date:** [May 2, 2018, 10:09am UTC](https://discuss.kotlinlang.org/t/safe-call-operator-chain/7650/4 "2018-05-02T10:09:33Z")

</div>

`toString()` is an extension function that can handle `null`: [toString - Kotlin Programming Language](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/to-string.html)

---

<div class="post-metadata">

**Author:** ![SackCastellon](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sackcastellon/32/4553_2.png) [@SackCastellon](https://discuss.kotlinlang.org/u/SackCastellon)\
**Post date:** [May 2, 2018, 10:39am UTC](https://discuss.kotlinlang.org/t/safe-call-operator-chain/7650/5 "2018-05-02T10:39:40Z")

</div>

The [documentation](https://kotlinlang.org/docs/reference/null-safety.html#safe-calls) says:

> ```
> b?.length
> 
> ```
> 
> This returns `b.length` if `b` is not null, and `null` otherwise. The type of this expression is `Int?`

So if we apply this to your code, and to make it easier to understand we unchain the calls, we have the following code:

```
val op1: Int? = a?.b
val op2: String = op1.toString()
val op3: Int = op2.length

println(op3)

```

The _safe call_ does not stop execution as soon as it finds a null value, it simply skips that call and returns null, so if you want to print `null` if `a == null` you should write it like this: `println(a?.b?.toString()?.length)`

Usually, if you make a safe call, all the chained calls following the first one need to be safe calls as well, but as @jstuyts pointed out:

> [@jstuyts](#):
>
> `toString()` is an extension function that can handle `null`
