I was wondering... Why does `fun String?.orEmpty()...
# stdlib
l
I was wondering... Why does
fun String?.orEmpty(): String
was not defined for
CharSequence
? Is it a multiplatform constraint?
r
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
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
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
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...