https://kotlinlang.org logo
Title
n

nkiesel

04/21/2023, 3:43 PM
Why is there a isNullOrEmpty for Array<Int> but not for IntArray in stdlib?
j

Joffrey

04/21/2023, 3:49 PM
I honestly don't know, but the question IMO should be why is there this function at all? Not sure how useful it can be. Using a nullable collection usually implies that you care about the difference between null and empty, so why use this function at all?
n

nkiesel

04/21/2023, 4:19 PM
This comes up often when dealing with Java APIs which are out of control
j

Joffrey

04/21/2023, 4:21 PM
But then you just call the java API and use
?: emptyList()
, basically dealing with java issues at the java boundary. It's good practice to get rid of platform types quickly, so we might as well deal with null there
n

nkiesel

04/21/2023, 4:26 PM
I get a Java class which has 3 such lists or arrays. For most of them, I can use
if (!j.a.isNullOrEmpty()) process(j.a)
without having to create empty collections. But does not work for IntArray or BooleanArray
But really, stdlib should be consistent. This method exists for most collections, but not for IntArray or BooleanArray. And to these should per my understanding be just slightly more efficient implementations of Array<Int> or Array<Boolean>, so I would expect the same set of convenience methods
j

Joffrey

04/21/2023, 4:32 PM
Yes I agree with the consistency argument. My point was that we should get rid of those entirely to make it consistent rather than add the missing ones.
Now for your specific case, if you really need those you can very easily write your own, or just use
array == null || array.isEmpty()
n

nkiesel

04/21/2023, 4:47 PM
Of course I could (and did) work around this limitation
And of course removing established methods from stdlib is a very long winded and expensive process
e

ephemient

04/21/2023, 9:11 PM
array?.isEmpty() != false
n

nkiesel

04/22/2023, 1:20 AM
Yeah, used
array?.isEmpty() == true
, but still surprised by the inconsistency
e

ephemient

04/22/2023, 1:21 AM
== true
means
null
false
, which isn't what
isNullOrEmpty()
does
n

nkiesel

04/22/2023, 1:22 AM
Right, I just looked at my code and I actually do have != false. Thanks
j

Joffrey

04/22/2023, 5:12 AM
And that is why using
array == null
in a separate condition is probably better. Shorter is not necessarily more readable.
e

ephemient

04/22/2023, 6:30 AM
I think
!= false
reads perfectly fine.
== null ||
doesn't work for more complex expressions (without binding a name specifically for it)