Elvis Operator
Sometimes, when we have a reference, we want to return some default value from the operation if the reference holds a null. To achieve that, we can use an elvis (?:) operator. This is an equivalent of orElse/orElseGet from Java Optional class:
val value: String? = null
val res = value?.length ?: -1
assertEquals(res, -1)
When the value reference holds a non-nullable value, the method length will be invoked:
val value: String? = "name"
val res = value?.length ?: -1
assertEquals(res, 4)