You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
220
221
221
222
```kotlin
222
223
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
247
248
248
249
```kotlin
249
250
funmain() {
251
+
250
252
//sampleStart
251
253
// Assigns null to a nullable variable
252
254
val b:String?=null
@@ -259,6 +261,21 @@ fun main() {
259
261
```
260
262
{kotlin-runnable="true"}
261
263
264
+
```kotlin
265
+
funmain() {
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
+
262
279
If the expression to the left of `?:` is not `null`, the Elvis operator returns it. Otherwise, the Elvis operator returns the expression
263
280
to the right. The expression on the right-hand side is evaluated only if the left-hand side is `null`.
0 commit comments