February 13, 2005
With the code posted here, the compare operator overload function doesn't work
for both array.sort and the compare operators.  When the code listed here is
compiled this error is given: SortFiles4.d(10): function
SortFiles4.main.YearName.opCmp must be declared as extern (D) int
opCmp(YearName*).

If opCmp's declaration is changed to that of the error message, then the
following errors are given:
SortFiles4.d(27): function SortFiles4.main.YearName.opCmp (YearName *o) does not
match argument types (YearName )
SortFiles4.d(27): cannot implicitly convert expression pair[1] of type YearName
to YearName *

However, if opCmp's declaration is left as it's posted here, but the line: pair.sort is commented out, it compiles clean.  ?  Not sure if defining opCmp for both op overload and the .sort array property is not supported.  But with different opCmp definitons, it can be made to work for both (which is good, just not at the same time).  Is there any way to have one definition that works for both?  Is defining a compare function for the .sort property officially supported for all array types?  I see it in the doc for AA's, but nothing explicit for the other types, though it can be made to work.

I think a consistent function definition that would work for both .sort and <> operators would be the best solution IMHO.

#import std.stdio;
#
#void main()
#{
#    struct YearName
#    {
#        int year;
#        char[] name;
#
#        int opCmp(YearName o)
#        {
#            if (year < o.year)
#                return -1;
#            else
#                return 1;
#        }
#    }
#
#    YearName[] pair;
#    pair.length = 2;
#    pair[0].year = 1987; pair[0].name = "Joe";
#    pair[1].year = 1981; pair[1].name = "Fred";
#
#    pair.sort;
#
#    if (pair[0] < pair[1])
#        writef("op overload works");
#}

-Kramer


February 14, 2005
Kramer wrote:
> With the code posted here, the compare operator overload function doesn't work for both array.sort and the compare operators.  When the code listed here is compiled this error is given: SortFiles4.d(10): function SortFiles4.main.YearName.opCmp must be declared as extern (D) int opCmp(YearName*).
<snip>

See also (AAs have the same problem):

http://www.digitalmars.com/drn-bin/wwwnews?digitalmars.D.bugs/2642

A workaround is to define two versions of opCmp.  But indeed, you shouldn't have to.

Stewart.

-- 
My e-mail is valid but not my primary mailbox.  Please keep replies on the 'group where everyone may benefit.