https://kotlinlang.org logo
Title
h

Hullaballoonatic

09/18/2018, 12:14 AM
i can't be the only one that thinks the constant casting between ints, doubles, etc in arithmetic kinda ruins the beautiful readability of kotlin
h

hudsonb

09/18/2018, 12:35 AM
Have an example of when it's ugly in arithmetic?
h

Hullaballoonatic

09/18/2018, 12:39 AM
how about a^2 + b^2 = c^2:
fun Pair<Int,Int>.distanceTo(to: Pair<Int,Int>): Float = sqrt((this.first - to.first).toDouble().pow(2.0) + (this.second - to.second).toDouble().pow(2.0)).toFloat()
the important things to consider in arithmetic are the mathematical operators, but the casting dominates the lines
especially when working with java classes that have the autocasting, so set field types to whatever number type, then in kotlin you have to constantly cast to it
h

hudsonb

09/18/2018, 1:23 AM
So in that example, you could add a
pow
extension function to
Int
right and avoid all the conversion calls? Or are ya looking for a lib that has already defined many extensions like that on numeric types?