What about adding this overload? ```operator fun &...
# stdlib
c
What about adding this overload?
Copy code
operator fun <T> List<T>?.plus(element: T) =
    if (this != null) this + element
    else listOf(element)
I've seen
(nullableList ?: emptyList()) + element
being used in a few places, and I don't think it's particularly readable (and it adds some useless logic when concatenating the empty list).
m
You can use
nullableList.orEmpty()
c
Oh, that's good to know. It will still concatenate the empty list with a single element, but it is more readable
k
This doesn't have the "useless logic" that you mention, but is slightly less readable:
Copy code
list?.plus(elem) ?: listOf(elem)
👍 1