opEquals does not work?

Ali Çehreli acehreli at yahoo.com
Fri Jun 7 14:53:44 PDT 2013


On 06/07/2013 02:18 PM, Jonathan M Davis wrote:

 > I have an open pull request as part of the move to getting rid of 
opEquals,
 > opCmp, toHash, and toString from Object, and it would make it so that you
 > could use something other than Object:

I hope it supports delegate-taking toString overload for classes.[1]

Ali

[1] As a reminder to others who don't know or keep forgetting like me, 
:) toString has a more efficient toString overload. Unfortunately, it 
works only with structs at this point.

I am way off-topic now but here is an example:

import std.stdio;
import std.format;

struct Point
{
     int x;
     int y;

     void toString(void delegate(const(char)[]) sink) const
     {
         formattedWrite(sink, "(%s,%s)", x, y);
     }
}

struct Color
{
     ubyte r;
     ubyte g;
     ubyte b;

     void toString(void delegate(const(char)[]) sink) const
     {
         formattedWrite(sink, "RGB:%s,%s,%s", r, g, b);
     }
}

struct ColoredPoint
{
     Color color;
     Point point;

     void toString(void delegate(const(char)[]) sink) const
     {
         formattedWrite(sink, "{%s;%s}", color, point);
     }
}

struct Poligon
{
     ColoredPoint[] points;

     this(ColoredPoint[] points)
     {
         this.points = points;
     }

     void toString(void delegate(const(char)[]) sink) const
     {
         formattedWrite(sink, "%s", points);
     }
}

void main()
{
     auto poligon = Poligon(
         [ ColoredPoint(Color(10, 10, 10), Point(1, 1)),
           ColoredPoint(Color(20, 20, 20), Point(2, 2)),
           ColoredPoint(Color(30, 30, 30), Point(3, 3)) ]);

     writeln(poligon);
}

The advantage of this method over the more-common toString() overload 
(the one that returns string) is that although there are still a total 
of 10 calls made to various toString() functions, those calls 
collectively produce a single string, not 10.

Ali



More information about the Digitalmars-d-learn mailing list