Make objects.opEquals a template?

Yuxuan Shui via Digitalmars-d digitalmars-d at puremagic.com
Mon Jul 13 02:13:05 PDT 2015


On Monday, 13 July 2015 at 09:10:41 UTC, ketmar wrote:
> On Mon, 13 Jul 2015 06:49:09 +0000, Yuxuan Shui wrote:
>
>> The main reason here is to use '==' in @safe code:
>> 
>> class A {
>> 	override @safe bool opEquals(Object o) {
>> 		return cast(A)o !is null;
>> 	}
>> }
>> @safe void main() {
>> 	A a = new A;
>> 	A b = new A;
>> 	a == b;
>> }
>> 
>> This doesn't compile in current D, because objects.opEquals 
>> takes two Object, and Object.opEquals is not @safe.
>> 
>> Why can't we have a template objects.opEquals?
>> 
>> auto opEquals(T, S)(T a, S b) { ... }
>
> 'case templates can't be virtual functions, so the following 
> will not work:
>
>   bool cmp (Object a, Object b) { return a.opQeuals(b); }
>
>   MyObj a, b;
>
>   cmp(a, b);

What do you mean by virtual function? objects.opEquals is not a 
member function.


More information about the Digitalmars-d mailing list