www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - range.chunks(2) error

reply Salih Dincer <salihdb hotmail.com> writes:
Hi,

When I use the chunks() template with iota(), for instance, with 
chunks(2), I can access both r.front and r.back. However, in a 
range of my own type (named iras in the code below), only r.front 
is working. I think the error given by r.back is not a bug 
related to chunks, is it?

```d
import std.array, std.algorithm,
        std.range,
        std.stdio;

void main()
{
   auto rng1 = iota!real(24.0, 1321.0, 16.5).take(8);
   auto rng2 = iras!real(24.0, 1321.0, 16.5).take(8);

   auto noError = rng1.chunks(2)
                      .map!(r =>
                       r.back - r.front);

   assert(noError.equal([16.5, 16.5, 16.5, 16.5]));
   /*
   auto error = rng2.chunks(2)
                      .map!(r =>
                       r.back - r.front);/*

     main.d(18): Error: none of the overloads of template
           `std.range.primitives.back` are callable using
            argument types `!()(Take!(InclusiveRange))`*/
}
/*
  * iras v3
  * (inclusiveRange) Source:
  * 
https://forum.dlang.org/post/bnnxentwstkjnxkychro forum.dlang.org
*/
```

The same problem occurs here:

```d
struct Range(T)
{
   T n = 3;
   bool empty;

   alias back = front;
   auto front() => n.cube - n * 0.5;

   auto popFront() => n += 2;
   auto backFront() => n -= 2;
}
```
SDB 79
Mar 28
parent Salih Dincer <salihdb hotmail.com> writes:
On Thursday, 28 March 2024 at 17:50:17 UTC, Salih Dincer wrote:
 Hi,

 When I use the chunks() template with iota(), for instance, 
 with chunks(2), I can access both r.front and r.back. However, 
 in a range of my own type (named iras in the code below), only 
 r.front is working. I think the error given by r.back is not a 
 bug related to chunks, is it?
It's really nice to solve a problem on your own. Turns out I needed to add save, opSlice, length and opDollar members to the range. SDB 79
Mar 28