ghedeon
05/08/2019, 11:31 PM<
operator for until
function. Gradually, having half-open and partial ranges, the slices syntax should follow.
Given:
Already implemented "closed range" in Kotlin.
Since Kotlin range is closed by default, by likeness, Groovy and Swift might be considered as an inspiration:
Closed range:
Swift: 0...5
Groovy: 0..5
Half-open range:
Swift: 0..<5
, ...<5
// compare to val a = until 5 ???
Groovy: 0..<5
Slice:
Swift: arr[1...5], arr[1..<5]
Groovy: arr[1..5]
======================================
Why it's important, why not until
, why partial range?
Personally, I believe that the closer we can get to the formal requirement (in this case — mathematical interval) the better. Thus, you don't execute the code but the requirement itself (it is only because of imperfection of our reality we're forced to write code around formal requirements 😉). That's why +
is better than plus()
. The former is essential and the latter is induced workaround.
Also imho, with KISS in mind, there is no need to reinvent slices syntax (I've seen :
proposal) when it's already covered by good range system. But it's a different discussion and I might be missing a bigger picture.
Does (1..<5)
look reasonable to you?
cc: @Dico, @Mike, @karelpeeters, @Hullaballoonatictodd.ginsberg
05/09/2019, 12:40 AMDanny Lamarti
05/09/2019, 7:58 AMval x = 0..3
val y = 0..<4
val z = 0 until 4
karelpeeters
05/09/2019, 8:12 AM0<..4
and 0<..<4
too for the other half open and open ranges.karelpeeters
05/09/2019, 8:12 AMDico
05/09/2019, 11:14 AM0>..<4
but it makes more sense to have the arrows the same way 😅Dico
05/09/2019, 11:15 AM0 < x < 4
karelpeeters
05/09/2019, 11:16 AMDico
05/09/2019, 11:16 AMdalexander
05/09/2019, 12:15 PMfor(i in 0..<5) { ... }
instead of
for(i in 0 until 5) { ... }
is worth adding another operator to the language.
Are there any syntactical cases other people run in to where ..<
disambiguates or simplifies an expression by maybe removing parentheses, for example? I'm not sure this is comparable to common mathematical operators like +
, because if you're working with something like a BigInteger the readability gains are obvious.Marc Knaup
05/09/2019, 1:54 PMMarc Knaup
05/09/2019, 1:55 PMghedeon
05/09/2019, 2:19 PMghedeon
05/09/2019, 2:28 PMvoddan
05/09/2019, 2:30 PMMike
05/09/2019, 2:32 PMrepeat(5) {}
and use it
in the block, although I didn’t realize repeat passed in the index until this thread.karelpeeters
05/09/2019, 2:33 PMrepeat
insinuates that the code inside it does about the same thing every time and is not dependent on the index.ghedeon
05/09/2019, 3:10 PMuntil
. And it scales further to slices and partial ranges. Not sure how one would express a partial range like ..<n
with until
. Now, if I got it right, we have a case where until
is not enough for date and time periods (see @Marc Knaup's link). Plus, it's shorter and self-explanatory, probably the reason it's used in other languages. So yeah, feel free to throw at me all the special cases where you feel like until
is unnatural or hard to reason about.Marc Knaup
05/09/2019, 3:23 PMuntil
is just a hack to partially support half-open range for types whose values have a natural predecessor or successor (e.g. 0 <-> 1 <-> 2) - by turning them into a normal Range
using `to`'s predecessor.
That means it won't work for example for Float, Double, String, dates, times and other potentially complex Comparable
types which don't have a clear predecessor.
E.g. what's the predecessor of 12pm? 11:59? 115959? 115959.999? 115959.999999999? …
Or for "abc"
? "abb"
? "abbz…"
? …
until
also doesn't work for minimum to
values.
999 until Int.MIN_VALUE
would result in an artificial range 1 to 0
which can be totally unexpected.
While it doesn't make sense to use MIN_VALUE
like in this example, in a dynamic scenario to
could be passed MIN_VALUE
which should result in a proper empty range with the start
unaltered.voddan
05/10/2019, 10:12 AMuntil
was specifically made for using in loops `for(i in 0 until length)`because that was the most common use case for open ranges in other languages. It was never supposed to substitute true open ranges because there wasn't any evidence that open ranges are useful outside of some narrow niches.voddan
05/10/2019, 10:13 AMhow
to do open ranges. But why
is far more important.voddan
05/10/2019, 10:16 AMvoddan
05/10/2019, 10:17 AMa < x < b
voddan
05/10/2019, 10:24 AMa..<b
. In both of those discussions your "why" is extremely important and has to be proven case-by-case. "Nice to have" or "others have it" is usually not good enough.Marc Knaup
05/10/2019, 10:27 AMHalfOpenRange
but almost always for temporal ranges.
I'm fine with it being an stdlib feature instead of a language feature with own syntax. Just some kind of standard would be great.
I agree that it doesn't necessarily need an update to the language itself.
I don't have any experience in using partial ranges so I can't say much about these.ghedeon
05/10/2019, 10:49 AMa..<b
." Like, I understand that it's an added functionality that not everyone is interested in, but how one can get an extremely bad experience from a..<b
and not from a until b
?chalup
05/10/2019, 11:08 AMDico
05/10/2019, 1:10 PM..
for half open ranges because they are simply way more common than the closed range you get with ..
. I'm reminded everytime I write a for loop over a range.Dico
05/10/2019, 1:12 PMvoddan
05/10/2019, 1:35 PMDico
05/10/2019, 8:57 PMvoddan
05/10/2019, 9:16 PMDico
05/10/2019, 9:22 PMMike
05/11/2019, 2:21 AM