You can do return listA.orEmpty() + listB.orEmpty(), seems easy enough.
The problem I see with the suggestion is, that for a nullable collection, being null and being empty could mean very different things. Therefore, it might be better to be implicit with your intentions.
the only issue i see if elements are nullable then itâs not clear what + should do, join collections or add a ânullâ element. Ok then maybe support âunionâ opearator instead, as it has works on 2 collections (and not individual elements).
E.g. having null + null == null, but null + empty == empty seems to be inconsistent to me: If null had some special meaning in this context, it is lost in the second expression. I think that nullable collections should be rarely used, and that you should avoid them when an empty collection does the job, especially because this kind of ambiguities.
Also, + is never used this way for other types, e.g. you canât write 1 + null, and even for String?, where it is defined, you have null + null == "nullnull".
I agree with @Landei . If we have only a single empty case, then from my experience it is much easier to use non-nullable empty collections. Often, this pattern results naturally with exactly the behavior we need. For example, we iterate over a collection and for empty case we need to not iterate - with null we need to handle it explicitly, empty collection does this naturally.
If we have two separate empty cases then using null vs empty collection is enigmatic and we probably need another approach anyway.
It doesnât. In both Java and Kotlin all empty collections created with utils like emptyList(), etc. re-use the same singleton object.
Iâve read âKotlin in Actionâ second edition, although maybe not very thorough, and what I understood is that null is fine and ok in Kotlin, therefore null collection is also fine.
You say that null collection does not make sense or they donât fit well. Where did you get that idea? Although I understand your point, iteration and other collection stuff does not work with null collections.
I donât say null collections donât make sense. You can use them and then do listA.orEmpty() + listB.orEmpty() as suggested above. Or even: (listA.orEmpty() + listB.orEmpty()).takeIf { it.isNotEmpty() } if you like to entirely avoid empty collections. I just shared my personal experience.
Generally, yes. What do you do if you need a number, but you donât have one? You can use null, and because the Kotlin type system keeps track of it, it is totally fine. In that sense, null is is fine. Now, would you use something like Optional<Int>?? Well, it looks at least a little weird, Optional has a perfectly nice way to express that you donât have a value. And I think similar about other collections. You can go even a step further, and use collections like NonEmptyList (from Arrow-Kt) to indicate that you always have at least one value.
The fact that Kotlin handles null way better than Java does is pretty cool, but it should be no excuse to use it just out of habit or laziness. In my personal code, you will find null values mainly on the âedgesâ, e.g. when dealing with the frontend or the database, especially when I have to use something like Spring. For the core functionality, I try to keep it null-free, or at least to resolve null as soon as possible.
Ahh, this is a good point. Apparently, Kotlin doesnât provide Collection + Collection operator, which could optimize for empty collections. Currently, it only provides Collection + Iterable.
As others have said, it depends what null means for a collection reference.
You could use it to mean an empty collection â but what would be the point? Thereâs already a better way to represent that (with an actual empty collection object), and so providing a second, inferior way to represent the same thing is pointless at best and often confusing/bug-prone.
Or, more likely, you could use it to mean something else â but there are many different, situaiton-specific things you could men, and then the way you want to combine lists will be specific to that, so no general solution is possible.
For example, I wrote a program in which a null set was taken to mean the universal set (âallâ), the opposite of the empty set (ânoneâ). It needed a few null checks, but it worked out pretty well and made good sense in context, and let me extend the intersection and union operators in a neat way. (But it wouldnât make sense in other contexts, of course.)