Why Kotlin needs downTo instead of just allowing a negative step?

Hey folks, I have a simple question but I can’t figure out for long while since I learned Kotlin…

Why does Kotlin needs a separate downTo infix instead of allowing the step to accept negative numbers? e.g. What is the counter example that a clause like 10 rangeTo 1 step -1 confuses the compiler?

2 Likes

10 rangeTo 1 is an empty range.
And step simply skips elements while trasversing a range, so it doesn’t take negative arguments.

The compiler isn’t confused or anything, those were explicit choices.

The are other ways to define those methods, and one could get away with fewer methods. It’s a choice for better readability imho

2 Likes