Can i rewrite methods in one line?

Ali Çehreli acehreli at yahoo.com
Sun Sep 2 19:40:37 PDT 2012


In general, yes, you can construct and return at the same time. As you 
said, in a language like D where source code is almost always visible, 
the compiler can apply many optimization techniques.

However, some of your operators ended up having semantic differences. 
opOpAssign used to return a reference to this object:

     ref Vector opOpAssign(string op) (Vector rhs)
         if(op == "+" || op == "-" || op == "*" || op == "/")
     {
         mixin("array[] "~op~"= rhs.array[];");
         return this;
     }

opOpAssign now returns a reference to a newly-created temporary object:

     ref Vector opOpAssign(string op) (Vector rhs)
         if(op == "+" || op == "-" || op == "*" || op == "/")
     { return Vector(mixin("array[] "~op~"= rhs.array[]")); }

(It is the same for the other opOpAssign.)

Ali


More information about the Digitalmars-d-learn mailing list