www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Operator overloading giving encrypted error messages.

reply Charles McAnany <mcanance rose-hulman.edu> writes:
Hi, all. I'm playing around with a BigRational struct to get more comfortable
with D.
The plan:
Allow binary operators so that a BR (BigRational) is the lhs and one of {BR,
BigInt, int, long} is the rhs.
So BR(5) + 2 compiles just as well as BR(5)+BR(2)
Here's the opBinary code I've worked out:
BigRational opBinary(string op,T)(T arg){
	BigRational y = BigRational(arg);
	static if(op == "+"){
		BigRational temp = this;
	}}} /// and so on, ending with an assert(0) after all the static ifs.

So, unit testing this with
auto br1 = BigRational(25,"2");
writefln("br1+1 = %s",br1+1);
works beautifully.


Now, I'd like to define the ++ to increase this by 1.
So, I write:
BigRational opUnary(string op)(){
	static if(op == "++"){
		this = this+1;
		return this;
//There are other overloads, too.
}}
Now, when I unittest
writefln("br1++ = %s", br1++); //This is line 166.
I get a weird error message:
C:\DProjects\BigRational\src>dmd BigRational.d -unittest
BigRational.d(166): Error: template BigRational.BigRational.opBinary(string op,T
) does not match any function template declaration
BigRational.d(166): Error: template BigRational.BigRational.opBinary(string op,T
) cannot deduce template function from argument types !()()

My issue here is that I have already used the exact same syntax in the first
unit test as my code for the ++ overload has, but here it fails me. (Plus, why
is line 166 having trouble with opBinary? ++ is unary, no?
Any ideas?
Thanks,
Charles.
begin 644 BigRational.d
M:6UP;W)T('-T9"YB:6=I;G0[#0II;7!O<G0 <W1D+G-T<FEN9SL-"FEM<&]R
M="!S=&0N<W1D:6\[#0II;7!O<G0 <W1D+FUA=& [#0HO+T$ 9&\M;F]T:&EN
M9R!M86EN+"!T;R!M86ME('1H92!C;VUP:6QE<B!H87!P>2X 5&AE<F4 :7, 
M82!W87D =&\ ;6%K92!A(&1L;"!O<B!S;VUE=&AI;F<L(&)U="!))VQL('-T
M:6-K('=I=&  86X 97AE+B`-"B\O5&\ 8V]M<&EL92!A;F0 <G5N('1E<W1S
M+"!D;60 0FEG4F%T:6]N86PN9"`M=6YI='1E<W0-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XH
M(G1H:7, /2!T:&ES(B!^;W` ?B`B('D[(BD["0T*"7)E='5R;B!T:&ES.PT*
M"7T-" D-" EI;G0 <VEG;B I>PT*"0EI9BAT:&ES+FYU;65R871O<B`^,"D-
M" D)"7)E='5R;B`Q.PT*"0EI9BAT:&ES+FYU;65R871O<B`]/2`P*0T*"0D)
M<F5T=7)N(#`[#0H)"7)E='5R;B`M,3L-" E]#0H)#0H)0FEG4F%T:6]N86P 
M86)S*"E[#0H)"7)E='5R;B!T:&ES*G1H:7,N<VEG;CL-" E]#0H)#0H)0FEG
M4F%T:6]N86P :6YV97)S92 I>PT*"0ER971U<FX 0FEG4F%T:6]N86PH=&AI
M<RYD96YO;6EN871O<BP =&AI<RYN=6UE<F%T;W(I.PT*"7T-" T*"4)I9TEN
M="!N*"E[#0H)"7)E='5R;B!T:&ES+FYU;65R871O<CL-" E]#0H)0FEG26YT
M(`ED*"E[#0H)"7)E='5R;B!T:&ES+F1E;F]M:6YA=&]R.PT*"7T-" D)#0I]
$#0H-" ``
`
end
Feb 08 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Charles McAnany:

 My issue here is that I have already used the exact same syntax in the first
 unit test as my code for the ++ overload has, but here it fails me. (Plus, why
 is line 166 having trouble with opBinary? ++ is unary, no?
 Any ideas?
In similar situations I suggest you to keep reducing your code until you have a minimal test case. Your code reduced: struct Foo { Foo opUnary(string op:"++")() { return this; } Foo opBinary(string op)(int y) { return this; } } void main() { auto f = Foo(); f++; } It sees the post-increment fires the opBinary template too. I think this is a front-end bug, for Bugzilla if confirmed and not already present. Bye, bearophile
Feb 08 2011
parent bearophile <bearophileHUGS lycos.com> writes:
 It sees the post-increment fires the opBinary template too. I think this is a
front-end bug, for Bugzilla if confirmed and not already present.
http://d.puremagic.com/issues/show_bug.cgi?id=5551
Feb 08 2011