Skip to content

Commit 3f2ff6d

Browse files
committed
tmp(docs); for fix test
1 parent 48821e7 commit 3f2ff6d

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

docs/topics/null-safety.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,9 @@ You can also place a safe call on the left side of an assignment:
215215
person?.department?.head = managersPool.getManager()
216216
```
217217

218-
In the example above, if one of the receivers in the safe call chain is `null`, the assignment is skipped, and the expression on the right is not evaluated at all. For example, if either
219-
`person` or `person.department` is `null`, the function is not called. Here's the equivalent of the same safe call but with the `if` conditional:
218+
In the example above, if one of the receivers in the safe call chain is `null`, the assignment is skipped, and the
219+
expression on the right is not evaluated at all. For example, if either `person` or `person.department` is `null`, the
220+
function is not called. Here's the equivalent of the same safe call but with the `if` conditional:
220221

221222
```kotlin
222223
if (person != null && person.department != null) {
@@ -247,6 +248,7 @@ Instead of writing the complete `if` expression, you can handle this in a more c
247248

248249
```kotlin
249250
fun main() {
251+
250252
//sampleStart
251253
// Assigns null to a nullable variable
252254
val b: String? = null
@@ -259,6 +261,21 @@ fun main() {
259261
```
260262
{kotlin-runnable="true"}
261263

264+
```kotlin
265+
fun main() {
266+
267+
//sampleStart
268+
// Assigns null to a nullable variable
269+
val b: String? = null
270+
// Checks for nullability. If not null, returns length. If null, returns a non-null value
271+
val l = b?.length ?: 100
272+
println(l)
273+
// 0
274+
//sampleEnd
275+
}
276+
```
277+
{kotlin-runnable="true"}
278+
262279
If the expression to the left of `?:` is not `null`, the Elvis operator returns it. Otherwise, the Elvis operator returns the expression
263280
to the right. The expression on the right-hand side is evaluated only if the left-hand side is `null`.
264281

0 commit comments

Comments
 (0)