www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can't overload opCmp for both array.sort and <> operators

reply Kramer <Kramer_member pathlink.com> writes:
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()


























-Kramer
Feb 13 2005
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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.
Feb 14 2005