Thread overview
Add inclusive range operator
Jul 25
jmh530
Jul 25
monkyyy
July 25

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.

July 25

On Friday, 25 July 2025 at 18:41:12 UTC, Brother Bill wrote:

>

These operators are in other languages.

What languages?

July 25

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!"[]"

July 25

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.

July 25

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[]);
}
July 26

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[]);
}
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:

x ..= y
x .. y+1

In general, I have not found the lack of this syntax to be an impediment.

-Steve

July 31

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.

It's not a bad idea, it's just that it's a nice-to-have that I particularly don't find that important.