Colton Idle
08/14/2020, 5:31 AMisNotNullOrEmpty
doesn't exist by default?
I hate having to do !something.isNullOrEmpty
tseisel
08/14/2020, 6:26 AMisNotEmpty
is what you're looking for. It also checks for null
.Colton Idle
08/14/2020, 6:28 AMLars Wenning
08/14/2020, 6:58 AMfun CharSequence?.isNotNullOrEmpty() : Boolean = !this.isNullOrEmpty();
this will make it possible to call the function as you wanted to:
fun main(){
var test : String? = null
test.isNullOrEmpty() //true
test.isNotNullOrEmpty() //false
}
Of course it is better to use to build in methods, but for future terms, if you experience similiar problems, this could be an option 🙂Colton Idle
08/14/2020, 7:02 AMnanodeath
08/14/2020, 2:06 PMisNotEmpty
doesn't check null. we have isNotEmpty
and also isNullOrEmpty
, which, besides the name, you can tell works because it operates on a Collection<T>?
. (docs)
so...you want !foo.isNullOrEmpty()
. you could make an extension method isNotNullOrEmpty()
, but IMO it's not necessary; tbh I'm not even sure why we have isEmpty()
and isNotEmpty()
. Maybe for concise method references in filters/sequences.Colton Idle
08/14/2020, 3:08 PM!
nanodeath
08/14/2020, 3:08 PMColton Idle
08/14/2020, 3:08 PMtseisel
08/15/2020, 12:42 PMisNotNullOrEmpty
extension function and .not()
is that you won't benefit from smart casts from String?
to String
(unlike !isNullOrEmpty
)nanodeath
08/15/2020, 2:59 PM