Printing a range of ranges drains them

Salih Dincer salihdb at hotmail.com
Tue May 28 05:54:36 UTC 2024


On Monday, 27 May 2024 at 16:28:28 UTC, monkyyy wrote:
> On Monday, 27 May 2024 at 00:25:42 UTC, Steven Schveighoffer 
> wrote:
>> 
>> So, does anyone expect this behavior? If so, can you explain 
>> why you think this is intentionally designed this way?
>
> This is correct behavior for ref front ranges with imperative 
> pop

Is the situation the same as this example?

```d
void main()
{
   class R
   {
     wchar* ptr;
     size_t len;

     this(T)(T[] range)
     {
       ptr = cast(wchar*)range.ptr;
       len = range.length;
     }

     auto empty() => len == 0;
     auto front() => *ptr++;
     auto popFront() => len--;
     auto save()
     {
       auto r = new R([]);
       r.len = len;
       r.ptr = ptr;
       return r;
     }
   }

   auto c = ['€', '₺', '₽'];
   auto r = new R(c);

   assert(!r.empty);

   import std.conv : text;
   auto str = r.text; // "€₺₽"

   assert(r.empty);
}
```

Okay, the objections are about inner ranges, but when you rewrite 
as  struct and remove the new operator while the R class is 
consumed above, a backup of the range is taken. Or is the 
difference between a class and a struct related to the reference 
type?

Thanks...

SDB at 79


More information about the Digitalmars-d mailing list