Reappearing error with sorting

nnk nospam at nospam.com
Fri Dec 25 15:16:03 PST 2009


It is quite strange, but the following code sometimes produces a
"core.exception.AssertError at C:\d\dmd2\src\phobos\std\array.d(253): Attempting
to fetch the front of an empty array" while trying to sort the array and
sometimes runs just fine:

import std.stdio,
       std.random,
       std.math,
       std.algorithm;

struct Point {
    double x, y;
    static Point opCall( double x, double y ) {
        Point nPoint;
        nPoint.x = x;
        nPoint.y = y;
        return nPoint;
    }
    double angle() {
        return (atan2( y, x ) / PI * 180);
    }
}

void printAngles( Point[] contour ) {
    foreach( Point p; contour )
        writef( "%0.1f ", p.angle() );
    writeln();
}

int main()
{
    Point[] path;
    path.length = 5;
    foreach( inout p; path )
        p = Point( uniform( 0, 100 ), uniform( 0, 100 ) );

    printAngles( path );
    sort!("a.angle() < b.angle()")(path);
    printAngles( path );

    return 0;
}

Using "a.x < b.x" as a sorting criterium produces normal behavior, so I guess
the problem is caused by using a function there. I'm new to D, and so I have
no idea, where exactly the source of this problem lies. Can somebody point me
in the right direction, please?

I'm using the version 2.037 of the compiler with phobos.


More information about the Digitalmars-d-learn mailing list