I’d like to have Range types for a couple of use cases:
- For loops:
for x in 0..10 {} - Contains checks of numbers:
0..10.contains(x)orx in 0..10 - Clamp API’s:
x.clamp(0..10) - Slicing:
[1,2,3,4].slice(0..2)
For simplicity, I think that each number type should just get its own range type, so u64 gets RangeU64. The alternative is Range<u64> but that’s difficult since we don’t have traits/interfaces.
There are of course some open questions:
- What should the syntax for exclusive ranges be?
- What should the syntax for inclusive ranges be?
- Are half open ranges (
0..and..10) allowed? - Are open ranges (
..) allowed?