https://kotlinlang.org logo
Title
j

Jakub Gwóźdź

10/12/2018, 2:34 PM
Yeah I was really surprised by it as recently I was playing with some maths and had this
data class Complex(val re: Double, val im: Double)
nice class and a few operators on it and I was so happy that I can do all this
z = z * z + c
thingies, wow Kotlin is so great. Then I was sooooo happy when I figured out I can add
val Double.i get() = Complex(0.0, this)
and simply have
val z = 3.5.i
And then, just after that, I saw the "Puzzlers" talk. And tried
-3.5.i
. Oh boy...
e

elizarov

10/12/2018, 2:35 PM
But what is the problem with that in your case? Just define
unaryMinus
operator on your complex number and it’ll work just as it is supposed to work.
j

Jakub Gwóźdź

10/12/2018, 2:36 PM
of course.
it was just a surprise
it works in my Complex class, wouldn't in the "toSting().length` case
e

elizarov

10/12/2018, 2:38 PM
It would had been really strange if the behavior of this code depended on the spae between
-
and
3.5
(in maths it does not usually matter if you have a space or not)
And tweaking priority is also inconsistent with math. Consider
-a.x-b.x-c.x
. Obviously, all
.x
shall execute before
-
operators (that is how it usually works in math)
j

Jakub Gwóźdź

10/12/2018, 2:42 PM
agree. I would only limit it to literals, but I get your point.