I've been thinking on how I would go about designing the "perfect" range literal if I were to do design a language. For you that don't know know a range literal in a statement that represents a range of values, like 1-4. They're most commonly used in for/foreach loops
There seems to a couple of issues one should take into account
Support for inclusive and exclusive ranges, tacking on +1 or -1 to endpoints seems a bit fugly and errorprone.
Support for stepping, so you can make a range of even or odd numbers for instance
Readability, it should be readily apparent what the range literal describes
Unambiguity, it should be perfectly unambigious what the range literal describes
The default should probably be from inclusive to exclusive since that's what's used in most cases for looping over arrays etc.
Anyways, one example of range literal I've seen is Ruby which is in the form of 1..3 for an exclusive (on the end) range and 1...3 for inclusive (on the end). You can also do 1..10.step(5). After careful consideration I found however a couple of things I didn't like about that approach (from my limited knowledge of ruby)
You can only describe inclusive and exclusive for the end. While describing most scenarios it does seem a little inconsistent.
Varying by just an additional . seems like a recipe for making it hard to see whether a range is inclusive or exclusive. I don't know about you but dot's tend to become something of a blur :)
Adding method like notation for ranges seems to mix the notion of a literal with that of a class which seems a bit inconsistent (even if ranges get compiled to a class)
Anyways, after pondering different alternatives. I came up with this
- [5..1] 5,4,3,2,1
- [1..5[ 1,2,3,4
- ]1..5] 2,3,4,5
- [0..5..20] 0,5,10,15,20
and so forth. I like it because [ normally denonates a set and this kinda fits into that, even though this in contrast to a set would be ordered.
One thing I'm a bit torn about though is making the exclusive/inclusive indicators mandatory or not, ie if you write just 1..5 the default would be 1,2,3,4 since it's the most common case with arrays etc. It's easier and more readable, but less specific and if you had to write [1..5[ you learn early about how they work.
So what do you think, did I cover most bases, overlook something? would you make the  mandatory? Would you design range literals differently in your programming language?
- bracket style: [0..10[ , with step: [0..5..20[
- interval notation: [0..10) with step: [0..5..20)
- exclamation for exlusive. 0..!10, with step: 0..5..!20
- with different step. 0..!20, 5
- however, that would make the default *0..10' inclusive-inclusive
- wordy: [0 to !20 by 5]
I must say that my favorite aestically so far is 0..!10 and 0..5..!20, I just wish the default 0..10 to inclusive-exclusive would be more logical