https://kotlinlang.org logo
#stdlib
Title
# stdlib
l

Luke

08/30/2021, 6:43 PM
I was wondering... Why does
fun String?.orEmpty(): String
was not defined for
CharSequence
? Is it a multiplatform constraint?
r

Ruckus

08/30/2021, 6:51 PM
I would guess it has to do with expected use. As most APIs (at least from my experience) work with strings, any use of
orEmpty
would then also require a call to
toString
in most any actual use case.
e

ephemient

08/30/2021, 7:02 PM
I wouldn't say that's true - many Android APIs take CharSequence, which can be annotated strings
but it's also pretty easy to write
?: ""
so it's not a huge missing gap
l

Luke

08/30/2021, 7:45 PM
Of course, it's something I could define myself, and absolutely minor in the standard library development. But I would think that the inclusion of
orEmpty
for String alone could justify the equivalent for
CharSequence
, or at least something like
fun <C, R> C?.ifNull(defaultValue: () -> R): R where C : R
, for functional programming style.
r

Ruckus

08/30/2021, 7:48 PM
That could be the case. I haven't done android in many years, so I guess my experience may come with a pretty strong sampling bias...
5 Views