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

WhatMeWorry kheaser at gmail.com
Tue Mar 11 16:56:28 UTC 2025


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"












More information about the Digitalmars-d-learn mailing list