opCmp and unittest as a nested function

bearophile bearophileHUGS at lycos.com
Wed Apr 9 02:55:10 PDT 2014


And you need to use opEquals:

unittest {
     static struct Point2D {
         double x, y;

         bool opEquals(const Point2D rhs) const {
             return false;
         }
     }

     auto p2d0 = Point2D(0, 0);
     auto p2d1 = Point2D(1, 0);

     assert(tween(p2d0, p2d1, 1 / 3.0) == Point2D(1 / 3.0, 0));
}


opEquals/onHash/opCmp are minefields. I don't know why the D 
compiler doesn't add a large amount of errors and warnings to 
turns this minefield in something a bit safer.

Also use std.math.feqrel to compare floating point values.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list