Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use less than < or greater > than in kotlin [duplicate]

Tags:

android

kotlin

How to use less than (<) or greater than (>) operator in kotlin?

I have checked comparedTo(other: Int?) function, but it only returns Int?.

class Adapter{
     private var mNewsCategories: List<NewsCategory>? = null
     //......
     val isAnything=  this.mNewsCategories?.size?.compareTo(0))
     //......
 }

The val isAnything returns another Int?. Actually, I need a Boolean variable.

Thanks in advance

like image 977
noobEinstien Avatar asked Feb 18 '18 11:02

noobEinstien


1 Answers

It’s not possible to use > on nullable types. If you consider null to map to the size 0, i.e. empty size, you can do:

val isAnything = (this.mNewsCategories?.size? ?: 0) > 0

While this will fix your problem, you should consider using isNotEmpty instead:

val isAnything = this.mNewsCategories?.isNotEmpty() ?: false

The Elvis Operator is explained here.

like image 71
s1m0nw1 Avatar answered Oct 26 '22 20:10

s1m0nw1