www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - opEquals on shared object

reply jj75607 <jj75607 gmail.com> writes:
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?
Jun 30 2016
parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Jun 30 2016
parent reply jj75607 <jj75607 gmail.com> writes:
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))
Jun 30 2016
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 6/30/16 8:30 AM, jj75607 wrote:
 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))
This is an artifact of the object.opEquals entry point function. It handles calling your object's opEquals properly. Try changing C.opEquals to accept a shared(Object). If that doesn't work, you may be out of luck until the druntime team figures this out. Please file an issue if you can't get it to work. -Steve
Jun 30 2016