I have a problem in Boolean class returning Boolea...
# announcements
n
I have a problem in Boolean class returning Boolean values when compareTo() is overridden and called. As we all know that compareTo() returns Int value whether it is zero, positive or negative. So how can Boolean class implicitly convert it to Boolean form ???????? When I tried to inherit Comparable class on user-defined class it printed same (i.e. Boolean value). Comparable Class : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-comparable/#functions Boolean Class : https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-boolean/compare-to.html
d
>
and
<
use
compareTo
, but they don't just call compareTo.
foo < bar
becomes
foo.compareTo(bar) < 0
n
But how they return Boolean instead of Int
d
foo.compareTo(bar) < 0
is a boolean.
foo.compareTo(bar)
is an int
👀 1
n
They return difference between given parameters
So how can it be Boolean s the return type mentioned is Int.
d
Are you even reading what I am saying?
😂 1
n
Could you please look at the Program screenshot
d
I did. You are not calling
compareTo
anywhere.
n
K
d
You are using
<
and
>
.
Version(...) > Version(...)
becomes
Version(...).compareTo(Version(...)) > 0
, that is a boolean.
👍 2
If you want the actual result of
compareTo
you need to use it directly:
Version(...).compareTo(Version(...))
, which is an int.
n
Ok I got it
m
please read about operator overloading to understand how it works
n
Actually It was returning Int result in println() function so I got bit confused how it can print Boolean value for that.