Thread overview
Integral types that define opCmp, opEquals
Mar 06, 2007
Henning Hasemann
Mar 06, 2007
BCS
Mar 06, 2007
Henning Hasemann
Mar 06, 2007
Frits van Bommel
Mar 06, 2007
Henning Hasemann
Mar 06, 2007
Frits van Bommel
March 06, 2007
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
March 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)
> Henning
> 

untested but it might work:

const T i;
is(i < i);


March 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
March 06, 2007
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)).
March 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
March 06, 2007
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"
}
---