Daniele Segato
09/14/2018, 4:18 PMin Int.MIN_VALUE until 0
I also know I can save the value and compare to 0 using a when
with no parameter.
But are these really the only ways? Can't I do something similar to the code below?
And to check !0 with ranges I would have to write:
!in in 1..Int.MAX_VALUE && !in -1 downTo Int.MIN_VALUE
But this doesn't look right, there must be another way, right?Andreas Sinz
09/14/2018, 4:22 PMin
, is
or a comparison ==
with an expression is supported by when
right nowDaniele Segato
09/14/2018, 4:26 PM-1 downTo Int.MIN_VALUE
is converted into 2 comparison while x < 0
would be only 1Shawn
09/14/2018, 4:26 PMAndreas Sinz
09/14/2018, 4:27 PMwhen
differently:
val x = functionReturningInteger()
when {
x < 0 -> { }
x > 0 -> { }
...
}
Daniele Segato
09/14/2018, 5:32 PMfunctionReturningInteger().let{ when { .... } }
val = ...
in my code, I usually prefer using let, apply, with and so on...
not sure if that's a good ideaShawn
09/14/2018, 5:34 PMDaniele Segato
09/14/2018, 5:35 PMreturn day.compareTo(other.day).let { if (it != 0) it else time.compareTo(time) }
then this:
val comparison = day.compareTo(other.day)
return if (comparison != 0) comparison else time.compareTo(time)
is it just me?Shawn
09/14/2018, 5:37 PMcomparison
over and over isn’t making it easier to readval x = ...
as a wholereturn day.compareTo(other.day)
.takeIf { it != 0 }
?: time.compareTo(other.time)
Alowaniak
09/14/2018, 7:56 PMcompareBy({it.day},{it.time}).compare(this,other)
afaict
Or compareValuesBy or w/eDaniele Segato
09/14/2018, 11:33 PMtakeIf
i didn't know about... Is there a list of all of this? Thanks by the way.
Also didn't know about compareBy!