opEquals on shared object

jj75607 via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 30 05:30:06 PDT 2016


On Thursday, 30 June 2016 at 12:21:03 UTC, Steven Schveighoffer 
wrote:
> On 6/30/16 6:26 AM, jj75607 wrote:
>> Hello!
>>
>> I need to overload opEquals on shared class C
>>
>> shared class C
>> {
>>     override bool opEquals(Object o) { return false; }
>> }
>>
>> But compilation fails with the message:
>> Error: function f700.C.opEquals does not override any 
>> function, did you
>> mean to override 'object.Object.opEquals'?
>>
>> What am I doing wrong?
>
> Object.opEquals is not marked shared. You can't override a 
> non-shared method with a shared one.
>
> You need to remove override.
>
> But... unfortunately, this may not work in practice. The 
> opEquals handling for objects is pretty much screwed unless you 
> have unshared mutable objects. I think it may work for const 
> objects, but not in a good way.
>
> -Steve

Thanks!

But what should I do to fix that code?

shared class C
{
     bool opEquals(Object o) { return false; }
}

class A(T)
{
     void f(T a, T b)
     {
         if(a == b)
             writeln("equals");
         else
             writeln("non equals");
     }
}

	
int main(string[] argv)
{
	auto a1 = new A!int;
	a1.f(1,2);

	auto a2 = new A!(shared(C));
	shared C c = new shared(C);
	a2.f(c,c);
	

	return 0;
}

It fails with
     Error: none of the overloads of 'opEquals' are callable using 
argument types (shared(C), shared(C))


More information about the Digitalmars-d-learn mailing list