www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Integral types that define opCmp, opEquals

reply Henning Hasemann <hhasemann web.de> writes:
Would it be possible/make sense to have int & co behave as if opCmp and opEquals
where defined for them?

That would make it easier in some situations to request for any type that
is comparable, i.e.

interface Comparable {
	int opCmp(object);
}

would match integer & co.

Alternative would be to have another way to determine if a type is comparable
maybe a a template-thingy (Im not fit enough with templates to say
if one could write such a thing himself without special handling of integral
types)

Henning

-- 
v4sw7Yhw4ln0pr7Ock2/3ma7uLw5Xm0l6/7DGKi2e6t6ELNSTVXb7AHIMOen5a2Xs5Mr2g5ACPR
hackerkey.com
Mar 06 2007
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Henning,

 Would it be possible/make sense to have int & co behave as if opCmp
 and opEquals where defined for them?
 
 That would make it easier in some situations to request for any type
 that is comparable, i.e.
 
 interface Comparable {
 int opCmp(object);
 }
 would match integer & co.
 
 Alternative would be to have another way to determine if a type is
 comparable
 maybe a a template-thingy (Im not fit enough with templates to say
 if one could write such a thing himself without special handling of
 integral types)
 Henning
 

untested but it might work: const T i; is(i < i);
Mar 06 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Henning Hasemann wrote:
  
 untested but it might work:

 const T i;
 is(i < i);

I tried this last week or so. Result is: found '<' when expecting ')' So it seem is() doesnt like "<".

IIRC 'is' tests if a _type_ is valid. Try something like is(typeof(i < i)).
Mar 06 2007
parent Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Henning Hasemann wrote:
 IIRC 'is' tests if a _type_ is valid. Try something like is(typeof(i < i)).

Works quite well, only thing is that it yields true for everything that is derived from object, but these indeed *have* defined opCmp so I guess there is no real way to find out if the writer of a class things this class' objects should be comparable to themselves.

No, AFAIK there indeed isn't, but there *is* a way to see if a class has an overridden Object.opCmp: (warning: dirty trick) --- import std.stdio; class Foo { // opCmp overridden int opCmp(Object o) { return 0; } } class Bar { } // opCmp not overridden void main() { writefln(&Foo.opCmp != &Object.opCmp); // prints "true" writefln(&Bar.opCmp != &Object.opCmp); // prints "false" } ---
Mar 06 2007
prev sibling next sibling parent Henning Hasemann <hhasemann web.de> writes:
 
 untested but it might work:
 
 const T i;
 is(i < i);
 

I tried this last week or so. Result is: found '<' when expecting ')' So it seem is() doesnt like "<". Henning -- v4sw7Yhw4ln0pr7Ock2/3ma7uLw5Xm0l6/7DGKi2e6t6ELNSTVXb7AHIMOen5a2Xs5Mr2g5ACPR hackerkey.com
Mar 06 2007
prev sibling parent Henning Hasemann <hhasemann web.de> writes:
 IIRC 'is' tests if a _type_ is valid. Try something like is(typeof(i < i)).

Works quite well, only thing is that it yields true for everything that is derived from object, but these indeed *have* defined opCmp so I guess there is no real way to find out if the writer of a class things this class' objects should be comparable to themselves. Henning -- v4sw7Yhw4ln0pr7Ock2/3ma7uLw5Xm0l6/7DGKi2e6t6ELNSTVXb7AHIMOen5a2Xs5Mr2g5ACPR hackerkey.com
Mar 06 2007