www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Proxy addition

reply "Dan" <dbdavidson yahoo.com> writes:
 From bug tracker I see that Proxy has a few issues, so this has 
likely been seen. But what would cause this error?

tmp/c.d(16): Error: overloads pure nothrow  safe double(auto ref 
CcRate b) and pure nothrow  safe double(auto ref CcRate b) both 
match argument list for opBinary

The error refers to two methods with the same signature. In 
typecons there are only two "Binary" methods, opBinary and 
opBinaryRight, both in Proxy, so where is the 
duplication/conflict coming from?

Thanks
Dan
---------------
import std.stdio;
import std.typecons;
import std.algorithm;

struct CcRate {
   private double rate = 0;
   mixin Proxy!rate;

   this(double rate) {
     this.rate = rate;
   }
}

unittest {
     CcRate r1 = 0.033, r2 = 0.002;
     writeln(r1+r2);  // compile error
}
---------------
Oct 25 2012
next sibling parent "Dan" <dbdavidson yahoo.com> writes:
On Thursday, 25 October 2012 at 16:39:57 UTC, Dan wrote:
 From bug tracker I see that Proxy has a few issues, so this has 
 likely been seen. But what would cause this error?

 tmp/c.d(16): Error: overloads pure nothrow  safe double(auto 
 ref CcRate b) and pure nothrow  safe double(auto ref CcRate b) 
 both match argument list for opBinary

 The error refers to two methods with the same signature. In 
 typecons there are only two "Binary" methods, opBinary and 
 opBinaryRight, both in Proxy, so where is the 
 duplication/conflict coming from?

 Thanks
 Dan
 ---------------
 import std.stdio;
 import std.typecons;
 import std.algorithm;

 struct CcRate {
   private double rate = 0;
   mixin Proxy!rate;

   this(double rate) {
     this.rate = rate;
   }
 }

 unittest {
     CcRate r1 = 0.033, r2 = 0.002;
     writeln(r1+r2);  // compile error
 }
 ---------------

Still trying to understand this. I found that if I change the following in Proxy it this example (r1 + r2) works fine. Plus the unit tests that are there still work. But, honestly I don't understand why...yet. Thanks, Dan ----- From typecons.Proxy ----- auto ref opBinary (string op, this X, B)(auto ref B b) { return mixin("a "~op~" b"); } ----- To ----- auto ref opBinary (string op, this X, B)(auto ref B b) if(!is(X == B)) { return mixin("a "~op~" b"); } -----
Oct 26 2012
prev sibling parent "cal" <callumenator gmail.com> writes:
On Friday, 26 October 2012 at 15:14:56 UTC, Dan wrote:
 Still trying to understand this. I found that if I change the 
 following in Proxy it this example (r1 + r2) works fine. Plus 
 the unit tests that are there still work. But, honestly I don't 
 understand why...yet.

 Thanks,
 Dan

 ----- From typecons.Proxy -----
     auto ref opBinary     (string op, this X, B)(auto ref B b) 
 { return mixin("a "~op~" b"); }
 ----- To -----
     auto ref opBinary     (string op, this X, B)(auto ref B b) 
 if(!is(X == B)) { return mixin("a "~op~" b"); }
 -----

From http://dlang.org/operatoroverloading.html, it is an error for both overloads of opBinary (opBinary and opBinaryRight) to match. Since the signatures for the two overloads in std.typecons are identical, this looks like a bug to me. Your modified version works (far as I can see) because it excludes one of the overloads from being matched, allowing the other to be used (opBinaryRight).
Oct 26 2012