Thread overview
Can somebody give me an example of using an "open interval for the upper limit of a range"?
March 11

I came across the following:

In D language, a dynamic array can be considered a range, as the "range" concept in D is a more general abstraction that encompasses any sequence of elements that can be iterated through, and dynamic arrays fit neatly into that category due to their ability to be accessed sequentially like a traditional array; you can use range-based for loops to iterate over a dynamic array directly.

In D language, an open interval for the upper limit is indicated by using a parenthesis ")" at the end of the range expression, meaning the upper bound value itself is not included in the interval.

To include the upper bound value, use a square bracket "]" at the end of the range."

foreach (element; range)
{
// Loop body...
}

So the following works:

int[] a;
. . .
foreach( i, e; a[0..3] )
{
write(i, ":", e); // 0:11 1:22 2:3
}

But a very naive D coder (me) tried something like

foreach( i, e; a[0..3) ) // Doesn't Compile! Maybe slice and not dynamic array?

So can someone provide an example of "open interval for the upper limit by using a parenthesis"

March 11
On 3/11/25 9:56 AM, WhatMeWorry wrote:

> In D language, an open interval for the upper limit is indicated by
> using a parenthesis ")" at the end of the range expression, meaning the
> upper bound value itself is not included in the interval.

The only place in D that I know of where that's the case is the std.random module. The following template argument has the quoted effect:

import std.stdio;
import std.random;

void main() {
    foreach (_; 0 .. 10) {
        writeln(uniform!"[)"(0, 3));
    }
}

The value 3 should never be printed there. (But "[)" happens to be the default for it anyway.)

Ali

March 12

On Tuesday, 11 March 2025 at 16:56:28 UTC, WhatMeWorry wrote:
[...]

>

To include the upper bound value, use a square bracket "]" at the end of the range."

[...]

>

foreach( i, e; a[0..3] )
write(i, ":", e); // 0:11 1:22 2:3

That example negates the claim of that Citation already!

Although a square bracket follows the range "0..3", neither the index '3´ nor the value of 'a[3]´ are remarked---and are not written indeed.

-manfred

March 13
On Wednesday, March 12, 2025 12:07:04 PM MDT Manfred Nowak via Digitalmars-d-learn wrote:
> On Tuesday, 11 March 2025 at 16:56:28 UTC, WhatMeWorry wrote: [...]
> > To include the upper bound value, use a square bracket "]" at the end of the range."
>
> [...]
> > foreach( i, e; a[0..3] )
> >     write(i, ":", e);  // 0:11  1:22  2:3
>
> That example negates the claim of that Citation already!
>
> Although a square bracket follows the range "0..3", neither the index '3´ nor the value of 'a[3]´ are remarked---and are not written indeed.

In mathematics and computer science in general, it's very common in the notation of ranges / intervals of numbers to use ] at the end of the range of values when the last number is inclusive, and ) when it's exclusive. So, I could definitely see some of the documentation using such notation when explaining things (though I don't recall seeing that anywhere off the top of my head). However, it definitely isn't something that's a syntactic part of how D handles anything. As Ali mention, it looks like std.random does use [ and ) in strings used as template arguments to specify the behavior of some functions, but that's probably the closest that we actually have to it being part of the language itself.

So, I don't know what part of the documentation that WhatMeWorry read which gave him the impression that the language itself uses ] or ) in any of its syntax to indicate how open an interval is, but it implies that either something we have was not written clearly enough (which is very possible) or that WhatMeWorry misinterpreted it in an unusual way.

- Jonathan M Davis