Why are static arrays not ranges?

Adam D. Ruppe via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Sep 21 13:39:44 PDT 2015


On Monday, 21 September 2015 at 20:33:10 UTC, Jack Stouffer wrote:
>     pragma(msg, isInputRange!(typeof(a)));

try:

      pragma(msg, isInputRange!(typeof(a[])));

Notice the addition of the [] after the a. That's the slicing 
operator and it will yield a range.

> Is there an actual reason for this?

A static array is a container; a block of memory. A range is a 
*view* into a container. When you popFront a range, you aren't 
modifying the underlying memory, you are just advancing the view 
to the next element. popFront of a static array is impossible 
anyway due to the immutability of its length, but even if it was 
possible, advancing your view to the next item should not 
actually delete the item if at all possible.

Dynamic arrays are actually similar btw, the underlying container 
for them is just hidden from view because the garbage collector 
manages it, so all you see is the slice (view/range).


More information about the Digitalmars-d-learn mailing list