Revised RFC on range design for D2

Sergey Gromov snake.scaly at gmail.com
Sat Sep 13 09:42:56 PDT 2008


Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> Sergey Gromov wrote:
> > Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> >> Given that in D const is transitive, we can't operate with const the 
> >> same way C++ does. Consider something as trivial as a copy:
> >>
> >> Tgt copy(Src, Tgt)(Src src, Tgt tgt)
> >> {
> >>      for (; !src.done; src.next) tgt.put(src.tip);
> >> }
> >>
> >> Problem is, you won't be able to copy e.g. an int[][] to another because 
> >> the types aren't compatible.
> > 
> > If Src is an input range you must make a deep copy.  This holds true for 
> > your current design also.  So this algorithm is flawed and it's good if 
> > it won't compile.
> 
> Great point. I need to sleep on this some more.

Then I'll continue to think aloud.

------------------------

Here are implementations of a byte-oriented input range on top of a file 
in both 2- and 3-component input range bases.  I thought 2-component 
version would be simpler.  They turned out to be almost equivalent.

class FileByteRange2 {
  private FILE* file;

  bool empty() {
    int c = fgetc(file);
    if (c == -1) {
      return true;
    }
    ungetc(c, file);
    return false;
  }

  ubyte next() {
    int c = fgetc(file);
    if (c == -1) {
      throw new Exception("File is empty!");
    }
    return cast(ubyte) c;
  }
}

class FileByteRange3 {
  private FILE* file;
  private bool cached;

  ubyte head;

  bool done() {
    if (!cached) {
      int c = fgetc(file);
      if (c == -1) {
        return true;
      }
      head = cast(ubyte) c;
    }
    return false;
  }

  void next() {
    cached = false;
  }
}

-------------------------

A nice pair for .retreat() is .advance().  The words are even of the 
same length.  Although I find it hard to imagine advancing or retreating 
a range.  Needless to say that "next" has nothing to do with ranges at 
all.

------------------------

Done is not the word.  Range is empty.  It's not good when a range is 
done for.
Besides, one can be done with a range before it's empty.


More information about the Digitalmars-d-announce mailing list