Returning an empty range of a given type

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 15 05:56:01 PDT 2015


On 5/14/15 11:22 PM, rcorre wrote:
> On Thursday, 14 May 2015 at 18:44:58 UTC, Steven Schveighoffer wrote:
>
>> It depends on the guts of MyContainer.Range.
>>
>> I'm assuming MyContainer.Range has SOME sort of references (i.e.
>> pointers) to the data in the container, so why not just have:
>>
>> bool empty() { return someRef == null || yourCurrentTest; }
>>
>
> In this case, I want to return an empty range when the container
> instance itself is null. I could have a static method
> MyContainer.emptySlice, but I feel like I've seen this general situation
> crop up a lot with small variations.

I'd have to see the code, but it seems like your situation is different 
from what I think, or you are not understanding what I'm saying :)

As an example:

struct MyContainer
{
   private int[] data;
   struct Range
   {
      private int *curData;
      private int *endOfData;
      int front() { return *curData; }
      bool empty() { return curData == endOfData; }
      void popFront() { ++curData; }
   }

   Range opSlice() {return Range(data.ptr, data.ptr + data.length);}
}

Range.init would be a valid empty range in this case. But I have no idea 
what your situation is. That's why I said it depends on the guts of the 
Range.

-Steve


More information about the Digitalmars-d-learn mailing list