digitalmars.D - Integral types that define opCmp, opEquals
- Henning Hasemann (14/14) Mar 06 2007 Would it be possible/make sense to have int & co behave as if opCmp and ...
- BCS (4/22) Mar 06 2007 untested but it might work:
- Henning Hasemann (8/13) Mar 06 2007 I tried this last week or so. Result is:
- Frits van Bommel (2/13) Mar 06 2007 IIRC 'is' tests if a _type_ is valid. Try something like is(typeof(i < i...
- Henning Hasemann (9/10) Mar 06 2007 Works quite well, only thing is that
- Frits van Bommel (15/23) Mar 06 2007 No, AFAIK there indeed isn't, but there *is* a way to see if a class has...
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
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) Henninguntested but it might work: const T i; is(i < i);
Mar 06 2007
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
Henning Hasemann wrote:IIRC 'is' tests if a _type_ is valid. Try something like is(typeof(i < i)).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 "<".
Mar 06 2007
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
Henning Hasemann wrote: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" } ---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.
Mar 06 2007