Hi @ilya.gorbunov
Pity about the arrays. I was at least hoping not to revert to the Java Array.equals but have a Kotlin specific comparison method (if == magic could not be performed):
Here is an excerpt of my very simple extensions:
infix fun Int?.eq(that: BigDecimal?) = this.toDec() eq that
infix fun Int?.lt(that: BigDecimal?) = this.toDec() lt that
infix fun Int?.gt(that: BigDecimal?) = this.toDec() gt that
infix fun Int?.ltEq(that: BigDecimal?) = this.toDec() ltEq that
infix fun Int?.gtEq(that: BigDecimal?) = this.toDec() gtEq that
infix fun String?.eq(that: BigDecimal?) = this.toDec() eq that
infix fun String?.lt(that: BigDecimal?) = this.toDec() lt that
infix fun String?.gt(that: BigDecimal?) = this.toDec() gt that
infix fun String?.ltEq(that: BigDecimal?) = this.toDec() ltEq that
infix fun String?.gtEq(that: BigDecimal?) = this.toDec() gtEq that
infix fun Long?.eq(that: BigDecimal?) = this.toDec() eq that
infix fun Long?.lt(that: BigDecimal?) = this.toDec() lt that
infix fun Long?.gt(that: BigDecimal?) = this.toDec() gt that
infix fun Long?.ltEq(that: BigDecimal?) = this.toDec() ltEq that
infix fun Long?.gtEq(that: BigDecimal?) = this.toDec() gtEq that
infix fun BigDecimal?.ltEq(that: BigDecimal?) : Boolean {
if (this == null && that == null)
return false
if (this == null || that == null)
return false
return this.compareTo(that) <= 0
}
fun BigDecimal?.between(lower: BigDecimal?, upper: BigDecimal?) : Boolean {
if (this == null && lower == null && upper == null)
return true
if (this == null)
return false
if (lower != null)
if (this lt lower)
return false
if (upper != null)
if (this gt upper)
return false
return true
}
fun BigDecimal?.notBetween(lower: BigDecimal?, upper: BigDecimal?) = !this.between(lower,upper)
fun String?.toDec() : BigDecimal? {
if (this == null)
return null
return BigDecimal(this)
}
A horde of other casting and comparison functions - similar to the examples above.
Which give me the chance to shorten code and for me, make it more readable than compareTo() e.g.:
object Test {
@JvmStatic
fun main(args : Array) {
val myDec = “1234.75”.toDec()
if (“1234.56” lt myDec && “1234.75” ne myDec)
println(“Case 1”)
else
println(“Case 2”)
}
}
A far out suggestion… is to handle decimals like C# maybe? The developer distraction and effort in C# decimals really is admirable. They handle like with L for Long M for Decimal (if I remember correctly); this while mathematics with these are natural across most types. Part of the niceness in the flow is that you don’t new it, which makes it feel like a primitive without the extra verbosity of creating new instances etc.
PS: Scala implicits used to drive me up the wall - but due to the lack of implicit casting in Kotlin one must find alternate ways to shorten and make better null-safety to types such as BigDecimal.