When would you use Elvis operator in Kotlin?

Technology CommunityCategory: KotlinWhen would you use Elvis operator in Kotlin?
VietMX Staff asked 3 years ago

The Elvis operator is part of many programming languages, e.g. Kotlin but also Groovy or C#. The Elvis operator is the ternary operator with its second operand omitted.

x ?: y // yields `x` if `x` is not null, `y` otherwise.

If x isn’t null, then it will be returned. If it is null, then the y will be returned.