I suspect most people feel it should be inclusive....
# announcements
m
I suspect most people feel it should be inclusive. I know I think that way. But obviously it's comfort and learning. Just like Java's substring takes time to learn. Generally people think it's inclusive of end index when it isn't.
d
I think the use case for inclusive ranges is WAY less common than exclusive ranges. I think it's a semantic decision assessing how best to distinguish between the two that resulted in this situation where most of the time you have to write
until
or
a..b-1
Personally, I find it very frustrating, I think .. should represent an exclusive range because of the use case being so much more common. It's counter intuitive with the style of indices starting at 0 rather than 1. But the window to change it has long faded.
👍 2
k
Nearly every range is exclusive,
subList
,
random.nextInt
, the most common for loop is
0, size
exclusive, etc. I like what Rust did more,
..
is exclusive and
..=
is inclusive. Too late to change now 🤷‍♂️
m
I think we can agree that both are very common. It's whatever way you've decided to approach it, and subjective. I also agree that the decision has been made.
g
I don't think it's too late. We just need to add
<
Swift: 0...5 & 0..<5 I like it even more than Rust version
2
d
That's a great idea @ghedeon, I'm happy to help with a KEEP
@Mike when considering all languages, both might be common, I still think exclusive is more common though. But in kotlin, no, inclusive ranges do not compare to exclusive ranges in terms of how common they are. Moreover, I would never even use
repeat
function if this design were the other way around.
m
I’m not seeing connection between repeat and substring. I’ve found people mess up substring in Java BECAUSE they think/assume it’s inclusive. So it’s opinion and YMMV situation all around. I’m happy to learn whatever the language I’m using decides to have.
k
repeat(10) { println(it) }
is 0 to 10 exclusive too.
m
I wasn’t aware that
repeat
passed an index in… Obviously it would be non-intuitive if it was inclusive as it would repeat n+1 times.
g
@Dico could be something. Have you done this before? Any well written KEEP from the top of your head so we can follow the structure? Let's push it.
d
@ghedeon well I wrote this KEEP https://github.com/Dico200/KEEP/blob/ibd-enhancements/proposals/implementation-by-delegation-enhancements.md But it hasn't had any answer from JB so probably not something to copy.
But the general structure is motivation and then solution in this case, and we're basically suggesting to add an
operator fun until
right?
@Mike the comparison is between
repeat(10) { i ->
and
for (i in 0..10) {
I would prefer to write the latter, but the problem is that it's not the same. I'd have to write
for (i in 0 until 10) {
even though the exclusive range provided by
until
is a much more common use case.
m
Personally, when I see 0..10, my brain instinctively thinks inclusive, so 0,1,2,…,10. And now that I know
repeat
passes in the index, I wouldn’t be inclined to use
for (i in 0..9)
rather than
repeat(10)
. But I agree it’s subjective, and support submitting a KEEP if you feel that strongly about it. Ultimately, it’s up to the Language team to decide.
d
We're not going to suggest changing it, that would break existing code. But something like ..< like @ghedeon suggested should be considered.
👍 1
k
@Dico Kind of off-topic but for what it's worth I really like that delegation proposal, there's a lot of good ideas in there that are very necessary. I hope it ends up getting somewhere.
d
Thank you @karelpeeters! ❤️