couple of really noob questions (ranges, toString)

Jesse Phillips jessekphillips+D at gmail.com
Mon Nov 1 14:57:42 PDT 2010


Hello and welcome.

The toString does work the same as it does in Java,  I suggest using override.

override string toString();

Your error actually indicates a bug in Phobos. It has a special print function for classes and for an input range. These are conflicting with each other and has nothing to do with your toString. Luckily once your container is properly created, you won't have this issue. So I will file the bug sometime.

The first thing to note is that you are creating a container[1] and not an InputRange[2]. The distinction here is that a Range is consumed, you don't iterate over them multiple times. A container on the other hand has different access requirements. (Documentation on Containers is a little messed up.)

Generally opApply is not used if you are building a Range, but does have its uses and I believe has priority in a foreach statement.

To build on the example from bearophile You would have something like this with your selection of Container functions:

import std.stdio: writeln;

class LinkedList(T) {
   override string toString() { return "foo"; }

   struct Range(T) {
      T front() { return T.init; }
      T moveFront() { return T.init; }
      void popFront() {}
      bool empty() { return true; }
   }

   Range!T opSlice() {
      Range!T r;
      return r;
   }
}
void main() {
    LinkedList!(int) list = new LinkedList!int;
    writeln(list);
    //writeln(list.toString());
}


1. http://digitalmars.com/d/2.0/phobos/std_container.html
2. http://digitalmars.com/d/2.0/phobos/std_range.html


More information about the Digitalmars-d-learn mailing list