downUntil


#1

Kotlin has .. for upward closed ranges, downTo for downward closed ranges, and until for upward half-open ranges.

I’d like to propose a new downUntil function for the only missing combination: downward half-open ranges. For example, for (i in 4 downUntil 0) print(i) would print “4321”.

(At Duolingo we disallow .. and downTo for consistency since none of our other programming languages use closed ranges. This leaves the rather awkward (lastNum until firstNum + 1).reversed() as our internal recommendation for downward ranges in Kotlin.)


#2

Just so I understand, you use (lastNum until firstNum + 1).reversed() instead of firstNum downTo lastNum just so you don’t have to use closed ranges?
I get that you want consistency between programs inside of the company but restricting language features like that feels kind of strange too me. But then I personally don’t switch between languages that often.

That being said, I would support this function being added to the standard library. It is the logical next step, as we already have downTo and until. You might want to create an issue for this here. I’m not sure whether this should go through the KEEP process. Anyways an issue is the best first step I guess.
Until this is part of the standard library I’d suggest you just create the function yourself for your projects.


#3

We don’t actually use that reversed snippet anywhere since we haven’t needed downward ranges yet, and we’ll probably end up creating our own downUntil if/when we do :slight_smile:

I’ve opened an issue at https://youtrack.jetbrains.com/issue/KT-24658