static array is not a range

Alexibu alex at sunopti.com
Tue Jan 9 11:13:23 UTC 2024


On Tuesday, 9 January 2024 at 10:44:34 UTC, Jonathan M Davis 
wrote:
> How would it even be possible for a static array to be a range? 
> It has a fixed length. For a type to work as a range, it needs 
> to be possible to pop elements off of it, which you can't do 
> with a static array. Input ranges must have front, popFront, 
> and empty. Dynamic arrays have that from std.range.primitivies 
> via UFCS (Universal Function Call Syntax), and that works, 
> because it's possible to shrink a dynamic array, but it won't 
> work with a static array, because its size will be fixed.
>
> Now, what you can do is slice a static array to get a dynamic 
> array which refers to the static array. And since dynamic 
> arrays work as ranges, you can use that with range-based 
> functions. That being said, you do then have to be careful 
> about the dynamic array (or any ranges which wrap it) escaping 
> from the scope where the static array is, because if the static 
> array goes out of scope and is destroyed, then any dynamic 
> arrays referring to it will be referring to invalid memory, and 
> you'll get undefined behavior. So, while slicing static arrays 
> can be very useful, it needs to be done with caution.
>
> - Jonathan M Davis

All good information, thanks.
I suppose I use ranges as things that can be arguments to 
algorithms in std.algorithm and std.range.

Although there is no state in the static array itself as you 
point out, couldn't we have a temporary input range created and 
then the compiler can elide it into whatever the foreach loop 
does.
So all the range algorithms could auto convert a static array 
into a range backed by the static array ?

Something like this : (although written by someone more competent)

```d

struct TempRange(X)
{
    x[n] * array;
    size_t i;
    this(static_array a)
    {
       array = a;
       i = 0;
    }
    X popFront() { return array[i]; }
    bool empty() { return i == array.length;}
}


R each(R,F)(R r,F f)
static if (isInputRange!R)
{
    normal implementation
}else if (isStaticArray!R)
{
    return TempRange(r).each(f);
}
```


More information about the Digitalmars-d-learn mailing list