couple of really noob questions (ranges, toString)

bearophile bearophileHUGS at lycos.com
Mon Nov 1 14:08:19 PDT 2010


Michael Woods:

> First question:  toString().  is this handled exactly the way that it is in Java?  I've written a toString method for my class, I'll post it below.  I can
> call this method, and it works perfectly, if called specifically.
> 
> linkedList!(int) list = new linkedList!(int)();
> writeln(list.toString());  //this works perfectly (if I add elements to the list, the results are the same)
> 
> writeln(list);  //this fails(at compile time) with a godawfully long error:

Welcome here. A reduced test case:


import std.stdio: writeln;
import std.range: InputRange;

class LinkedList(T) : InputRange!T {
    T front() { return T.init; }
    T moveFront() { return T.init; }
    void popFront() {}
    bool empty() { return true; }
    int opApply(int delegate(ref T) dg) { return 0; }
    int opApply(int delegate(ref size_t, ref T) dg) { return 0; }
    override string toString() { return "foo"; }
}
void main() {
    LinkedList!(int) list = new LinkedList!int;
    writeln(list);
    //writeln(list.toString());
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list