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


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-"G9O:60 ;6%I;B I>PT*

M<F5C:7-I;VX 87)I=&AM971I8R!O;B!N=6UB97)S(`T*=VET:"!N=6UE<F%T



M86QC=6QA=&5S('1H92!G8V0 ;V8 ='=O($)I9TEN=',L('5S:6YG($5U8VQI
M9"=S(&%L9V]R:71H;2X-" E":6=);G0 9V-D*$)I9TEN="!I;G0Q+"!":6=)


M<W-U;65S('1H870 8F]T:"!V86QU97, 87)E('!O<VET:79E+ T*"4)I9TEN



M96YO;6EN871O<B`]/2`P*7L-" D)"71H<F]W(&YE=R!%>&-E<'1I;VXH(D1I

M"0D);G5M97)A=&]R(#U

M=&EV92P 9FQI<"!T:&4 ;F5G871I=F4N(`T*"0EI9B`H9&5N;VUI;F%T;W( 


M;W(L9&5N;VUI;F%T;W(I.PT*"0EN=6UE<F%T;W( +ST 8V]M;6]N1F%C=&]R


M871I;VYA;"DH0FEG4F%T:6]N86P ;G5M+"!":6=2871I;VYA;"!D96XI>PT*

M871O<BQR97,N9&5N;VUI;F%T;W(I.PT*"7T-" D-" D-" ET:&ES*%0Q(#T 
M0FEG26YT+"!4,B`]($)I9TEN="DH0FEG26YT(&YU;2P 0FEG26YT(&1E;BE[

M=')I;F<L(%0R(#T <W1R:6YG*2AS=')I;F< ;G5M+"!S=')I;F< 9&5N*7L-



M"7T-" D-" EV;VED('-E=%5P4W1A=&4H0FEG26YT(&YU;2P 0FEG26YT(&1E

M=&]R(#T

M=&AE3G5M(#T 0FEG4F%T:6]N86PH;G5M*3L-" D)875T;R!T:&5$96X /2!"
M:6=2871I;VYA;"AD96XI.PT*"0EA=71O(')E<R`]('1H94YU;2]T:&5$96X[

M.PT*"7T-" D-" ET:&ES*%0 /2!":6=2871I;VYA;"DH0FEG4F%T:6]N86P 
M<W1A<G0I>PT*"0ES9715<%-T871E*'-T87)T+FYU;65R871O<BP <W1A<G0N
M9&5N;VUI;F%T;W(I.PT*"7T-" D-" D-" ET:&ES*%0I*%0 ;G5M*0T*"6EF
M*"%I<RA4(#T


M"0ES9715<%-T871E*'1H94YU;2Q":6=);G0H,2DI.PT*"7T-" D-" DO+U-T
M<F5S<R!T:&4 8V]N<W1R=6-T;W)S+B`-" EU;FET=&5S='L-" D)0FEG4F%T
M:6]N86P 8G(Q(#T 0FEG4F%T:6]N86PH(C$R,S0B+"(R-2(I.PT*"0E":6=2
M871I;VYA;"!B<C( /2!":6=2871I;VYA;"A":6=);G0H,C,T-2DI.PT*"0EA
M<W-E<G0H8G(R(#T



M=&EO;F%L*&)R,2Q":6=);G0H,C`P,"DI.PT*"0E":6=2871I;VYA;"!B<C4 
M/2!":6=2871I;VYA;"AB<C$L8G(R*3L-" D)875T;R!B<C8 /2!":6=2871I






M+G1O4W1R:6YG*"AC;VYS="AC:&%R*5M=(',I>VYU;4-H87)S('X](',[?2PB

M;6%T*"(E<R(L;G5M0VAA<G,I.PT*"0EC;VYS="AC:&%R*5M=(&1E;D-H87)S
M.PT*"0ED96YO;6EN871O<BYT;U-T<FEN9R H8V]N<W0H8VAA<BE;72!S*7MD


M=&\ 8G(Q(#T


M<C$I.PT*"7=R:71E;&XH8G(Q+G1O4W1R:6YG*3L-" EB<C$ +ST 8G(Q+S([

M*3L-" EB<C$ *CT ,#L-
M;BAB<C$N=&]3=')I;F<I.PT*"7T-" D-" D-" E":6=2871I;VYA;"!O<%5N
M87)Y*'-T<FEN9R!O<"DH*7L-" D)<W1A=&EC(&EF("AO<"`]/2`B*R(I>PT*

M" D)"7)E='5R;B!":6=2871I;VYA;" M;G5M97)A=&]R+"!D96YO;6EN871O



M"0ER971U<FX =&AI<SL-" D)?0T*"7T-" D-" EU;FET=&5S='L-" EA=71O

M<C$I"3L-" EW<FET969L;B B8G(Q*S$ /2`E<R(L8G(Q*S$I.PT*"7=R:71E
M9FQN*")B<C$K*R`]("5S(BP 8G(Q*RLI.PT*"7=R:71E9FQN*")B<C$ /2`E
M<R(L8G(Q*3L-" DO+W=R:71E9FQN*"(K*V)R,2`]("5S(BPK*V)R,2D["0T*
M"7T-" D-" D-" DO+T]V97)L;V%D('1H92!B:6YA<GD ;W!E<F%T;W)S(%`L

M86P ;W!":6YA<GDH<W1R:6YG(&]P+%0I*%0 87)G*7L-" D)0FEG4F%T:6]N
M86P >2`]($)I9U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^,"D-
M" D)"7)E='5R;B`Q.PT*"0EI9BAT:&ES+FYU;65R871O<B`]/2`P*0T*"0D)


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



`
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