Another question: Why doesn’t `ClosedRange` have o...
# stdlib
m
Another question: Why doesn’t
ClosedRange
have operators for destructuring?
m
Sounds crazy to me. Any specific use case?
m
I was writing something like this and for some reason expected it to work:
Copy code
somethingReturningClosedRange()
   .let { (start, endInclusive) -> someExternalFunc(start, endInclusive) }
   …
m
Copy code
val (a, b) = 1..10
print(a)
print(b)
What would you expect? 1 and 10, or 1 and 2?
Copy code
val (a, b) = (1..10).toList()
print(a) // 1
print(b) // 2
If you really want to have it, you can add it:
Copy code
operator fun <T: Comparable<T>> ClosedRange<T>.component1() = start
operator fun <T: Comparable<T>> ClosedRange<T>.component2() = endInclusive
But I don't recommend that
m
I have that already. Was just wondering why it wasn’t in stdlib. Interesting point that it’s ambiguous whether it should return
1, 10
or
1, 2
.
m
I think what you are looking for and what I would love to have as well is name-based destructuring that is just based on property names.