www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compound assignment operators

reply "Yao G." <yao.gomez spam.gmail.com> writes:
Is there a way to make compound assignment operators (-=, +=, *= and  
friends) work with D's operator overload regime? I can't make them work.

 struct Foo
 {
     this( int foo )
     Foo opBinary(string op)( in typeof(this) rhv )
     if( op == "-=" )
     {
          _bar -= rhv._bar;
          return this;
     }

     private int _bar;
 }

 void main()
 {
     au

-- Yao G.
Aug 25 2010
next sibling parent reply "Yao G." <yao.gomez spam.gmail.com> writes:
Sorry, I sent the message prematurely :( Anyway, here's complete:

Is there a way to make compound assignment operators (-=, +=, *= and
friends) work with D's operator overload regime? I can't make them work.

 struct Foo
 {
     this( int bar ) {
          bar = bar;
     }

     Foo opBinary(string op)( in typeof(this) rhv )
     if( op == "+=" )
     {
          _bar += rhv._bar;
          return this;
     }

     private int _bar;
 }

 void main()
 {
     auto f1 = Foo(1);
     auto f2 = Foo(2);
     f2 += f1;

I get the following compiler error:
 Error: 'f1' is not a scalar, it is a Foo
 Error: 'f1' is not of arithmetic type, it is a Foo
 Error: 'f2' is not of arithmetic type, it is a Foo

Is there a way to make this work? Even changing the operator string in opBinary to "-" doesn't do nothing. Or those kind of operators can't be overloaded? -- Yao G.
Aug 25 2010
parent bearophile <bearophileHUGS lycos.com> writes:
Yao G.:
 Is there a way to make this work? Even changing the operator string in  
 opBinary to "-" doesn't do nothing. Or those kind of operators can't be  
 overloaded?

Try opOpAssign. Bye, bearophile
Aug 25 2010
prev sibling next sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Wednesday 25 August 2010 22:31:38 Yao G. wrote:
 Sorry, I sent the message prematurely :( Anyway, here's complete:
 
 Is there a way to make compound assignment operators (-=, +=, *= and
 friends) work with D's operator overload regime? I can't make them work.

Look at http://www.digitalmars.com/d/2.0/operatoroverloading.html (or even better, TDPL). The correct function would be opOpAssign. I believe that the syntax for += would be opOpAssign!("+")(args) { } - Jonathan M Davis
Aug 25 2010
prev sibling next sibling parent "Yao G." <yao.gomez spam.gmail.com> writes:
On Thu, 26 Aug 2010 01:46:02 -0500, Jonathan M Davis  
<jmdavisprog gmail.com> wrote:

 Look at http://www.digitalmars.com/d/2.0/operatoroverloading.html (or  
 even
 better, TDPL). The correct function would be opOpAssign. I believe that  
 the
 syntax for += would be

 opOpAssign!("+")(args) { }

 - Jonathan M Davis

Ha ha ha! What a shame, I was reading that page looking for a solution, but it seems that I just skipped that part. Thanks and sorry for the stupid question. -- Yao G.
Aug 25 2010
prev sibling parent Jonathan M Davis <jmdavisprog gmail.com> writes:
On Wednesday 25 August 2010 23:49:27 Yao G. wrote:
 On Thu, 26 Aug 2010 01:46:02 -0500, Jonathan M Davis
 
 <jmdavisprog gmail.com> wrote:
 Look at http://www.digitalmars.com/d/2.0/operatoroverloading.html (or
 even
 better, TDPL). The correct function would be opOpAssign. I believe that
 the
 syntax for += would be
 
 opOpAssign!("+")(args) { }
 
 - Jonathan M Davis

Ha ha ha! What a shame, I was reading that page looking for a solution, but it seems that I just skipped that part. Thanks and sorry for the stupid question.

Yeah, well. It's a sparse page, and that section is quite small, so it's pretty easy to miss. Really, at this point, TDPL is the place to go to look stuff up unless it's Phobos-specific stuff. - Jonathan M Davis
Aug 26 2010