jeudi 3 décembre 2020

How a function that return an Int can be part of if statement?

I have define a new function (please don't refer to the logic, only if it is necessary):

 operator fun Int?.compareTo(other: Int): Int
        {
            if (this == null)
                return -1
            if (this > other)
                return 1
            else return 0

        }

now in an if statment i'm writing:

 val x:Int? =8
 if(... && .... && ...&& x > 5)

I see that the x>5 runs the function I wrote above (compare), but this function doesn't return a Boolean, it returns an Int. So how can it compiled although one of the condition isn't a boolean?

Aucun commentaire:

Enregistrer un commentaire