``` (argument as List<String>?)?.orEmpty() `...
# getting-started
p
Copy code
(argument as List<String>?)?.orEmpty()
intellij is highlighting the
?.
as "This call is useless" why is it useless?
t
orEmpty()
works on a nullable list
p
oh right
thank you
the fact that you can call a function (albeit only an extension function) on a
null
blows my mind
t
It’s one of the reasons we ♥️ K
❤️ 2
a
I might be wrong but what makes the ?. useless is the list be nullable itself and handled in other place of the where the code is implemented. Otherwise to avoid ?. would be emptyOrNull or maybe nullOrEmpty. I don’t memorize code.
p
The
?.
is useless as the
.orEmpty()
-function (https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/or-empty.html) is called on an instance of type
Collection<T>?
or
List<T>?
and returns an empty list in the case that the collection or list is
null
. Adding the safe call operator would cause
orEmpty()
to only be called when it is not null, which would be useless since the function’s purpose is to do something when it’s reciever is null.