== Quote from Jesse Phillips (jessekphillip...@gmail.com)'s article > On Sat, 17 Dec 2011 14:06:48 +0000, Heromyth wrote: > > I have defined a opCmp function to overload comparison operators, then > > how can I use it correctly? > > > > I can use it like this: > > int b = t1.opCmp(info); > > and is it right like this: > > int b = t1 < info; > Consider the code more to the point of: > int b = t1.opCmp(info); > bool c = t1 < info; That's it. Thanks, I got what I wanted. Here is the modified test code: // Test.d import std.stdio; import std.algorithm; import std.range; import std.array;
public class LogLevel { @property public static LogLevel Trace() { if( m_Trace is null ) m_Trace = new LogLevel("Trace", 0); return m_Trace; } private static LogLevel m_Trace; @property public static LogLevel Info() { if( m_Info is null ) m_Info = new LogLevel("Info", 2); return m_Info; } private static LogLevel m_Info; private int ordinal; private string name; public override int opCmp(Object o) { LogLevel level1 = cast(LogLevel) o; int result = 0; if( this.Ordinal > level1.Ordinal) result = 1; else result = -1; writefln("result == %d", result); return result; } @property package int Ordinal() { return this.ordinal; } private this(string name, int ordinal) { this.name = name; this.ordinal = ordinal; } } int main(string[] args) { LogLevel t1 = LogLevel.Trace; LogLevel t2 = LogLevel.Trace; LogLevel info = LogLevel.Info; bool a = t1 == t2; writefln("a == %s", a); int b = t1.opCmp(info); writefln("b == %d", b); b = t1 < info; writefln("b == %d", b); bool c = t1 > info; writefln("c == %s", c); c = t1 < info; writefln("c == %s", c); return 0; } /* a == true result == -1 b == -1 result == -1 b == 1 result == -1 c == false result == -1 c == true */