why does (0..0).forEach { ... } iterate once inste...
# announcements
d
why does (0..0).forEach { ... } iterate once instead of zero times?
e
Copy code
(0..0).toList() == listOf(0)
n
0 upto 0
should be the empty list
d
I can rephrase it. why does 0..0 not produce an empty list
e
0 until 0
is empty
n
because it's an inclusive end bound
👍 3
☝️ 4
c
A range defines a closed interval in the mathematical sense: it is defined by its two endpoint values which are both included in the range
https://kotlinlang.org/docs/reference/ranges.html#range
n
yeah, i was also surprised that kotlin made the simple thing closed. Hopefully this doesn't trigger a holy war 🙂
n
maybe they borrowed it from Ruby
d
even though i read it I didn't realize the implications for 0..0 but it makes sense. thanks
e
it's challenging to represent
Int.MIN_VALUE..Int.MAX_VALUE
without using inclusive ends
2
n
@ephemient that's a good point I hadn't considered, though I'm not sure that's the most common case (seems like in many of those use cases an infinite range would be simpler)
e
but generally it doesn't matter - all of the following are equivalent
Copy code
0 until list.size
0..list.lastIndex
list.indices
and with the last one you don't even need to think about whether the bounds are inclusive or exclusive
n
yeah, that's a good point
n
I think I've written
0..(list.size - 1)
before. feelsbad.
n
Ruby uses
..
for closed and
...
for open, which I didn't know. Kotlin I suppose could probably very easily choose to add
...
later... not that it's the highest priority
yeah, i've definitely size-1'ed too
e
in a way, Swift's
..<
for half-open intervals makes it obvious it's not closed, but... I think it's a bit ugly
spelling it
until
in Kotlin is fine with me