www.digitalmars.com         C & C++   DMDScript  

digitalmars.dip.ideas - Add inclusive range operator

reply Brother Bill <brotherbill mail.com> writes:
We have range 10..15 which has elements: 10, 11, 12, 13, 14 but 
not 15

Recommend operator ..= as in 10..=15 or operator ... as in 10...15
This would produce inclusive range of elements: 10, 11, 12, 13, 
14, 15

These operators are in other languages.

I am sure that I am not the first to recommend this.
Jul 25
next sibling parent reply jmh530 <john.michael.hall gmail.com> writes:
On Friday, 25 July 2025 at 18:41:12 UTC, Brother Bill wrote:
 
 These operators are in other languages.
What languages?
Jul 25
parent Lance Bachmeier <no spam.net> writes:
On Friday, 25 July 2025 at 18:46:04 UTC, jmh530 wrote:
 On Friday, 25 July 2025 at 18:41:12 UTC, Brother Bill wrote:
 
 These operators are in other languages.
What languages?
The operator being proposed is taken from Rust.
Jul 25
prev sibling next sibling parent monkyyy <crazymonkyyy gmail.com> writes:
On Friday, 25 July 2025 at 18:41:12 UTC, Brother Bill wrote:
 We have range 10..15 which has elements: 10, 11, 12, 13, 14 but 
 not 15

 Recommend operator ..= as in 10..=15 or operator ... as in 
 10...15
 This would produce inclusive range of elements: 10, 11, 12, 13, 
 14, 15

 These operators are in other languages.

 I am sure that I am not the first to recommend this.
I suggested a `x..y+1` special case before this section even existed; was told to use the enum members trait or iota!"[]"
Jul 25
prev sibling parent reply Lance Bachmeier <no spam.net> writes:
On Friday, 25 July 2025 at 18:41:12 UTC, Brother Bill wrote:
 We have range 10..15 which has elements: 10, 11, 12, 13, 14 but 
 not 15

 Recommend operator ..= as in 10..=15 or operator ... as in 
 10...15
 This would produce inclusive range of elements: 10, 11, 12, 13, 
 14, 15

 These operators are in other languages.

 I am sure that I am not the first to recommend this.
Given the poor design of the language for this type of thing, I don't think it would be worth adding new syntax until that's cleared up. ``` void main() { // Nope // long[] a = 1..4; // Nope // long[] a = [ 1..4 ]; import std.range: iota; // Nope // long[] a = iota(1, 5); import std.array: array; // Nope // long[] a = iota(1, 5).array; import std.conv: to; // Finally! long[] a = iota(1, 5).array.to!(long[]); } ```
Jul 25
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On Friday, 25 July 2025 at 19:42:53 UTC, Lance Bachmeier wrote:
 Given the poor design of the language for this type of thing, I 
 don't think it would be worth adding new syntax until that's 
 cleared up.

 ```
 void main() {
     // Nope
     // long[] a = 1..4;
     // Nope
     // long[] a = [ 1..4 ];
     import std.range: iota;
     // Nope
     // long[] a = iota(1, 5);
     import std.array: array;
     // Nope
     // long[] a = iota(1, 5).array;
     import std.conv: to;
     // Finally!
     long[] a = iota(1, 5).array.to!(long[]);
 }
 ```
```d auto a = iota(1L, 5L).array; ``` But yeah, I would like to see `x .. y` become an expression that reduces to a specialized type, and then we could have things like `array(1L .. 5L)` As for the OP's idea, I'm not fully against it. But what it replaces is quite sensible also. I'm not sure it's worth the extra feature: ```d x ..= y x .. y+1 ``` In general, I have not found the lack of this syntax to be an impediment. -Steve
Jul 25