Can i rewrite methods in one line?
Ali Çehreli
acehreli at yahoo.com
Sun Sep 2 20:44:24 PDT 2012
On 09/02/2012 07:57 PM, Ivan Agafonov wrote:
> ref Vector opOpAssign(string op) (T rhs) if(op == "+" || op == "-" || op
> == "*" || op == "/")
> { mixin("array[] "~op~"= rhs;"); return this; }
>
> But i can't write something like this:
> return Vector(mixin("array[] "~op~"= rhs.array[]"));
You meant it for opOpAssign, right?
opOpAssign must 'return this', not a new object. Otherwise chaining
operators would be confusing:
(v0 += v1) *= v2;
If the += operation could return a new object (which it can't), then *=
would be operating on that.
So your existing version is correct:
ref Vector opOpAssign(string op) (T[size] rhs)
if(op == "+" || op == "-" || op == "*" || op == "/")
{ mixin("array[] "~op~"= rhs[];"); return this; }
But if you don't care about chained operations you can also return void:
void opOpAssign(string op) (T[size] rhs)
if(op == "+" || op == "-" || op == "*" || op == "/")
{ mixin("array[] "~op~"= rhs[];"); }
Ali
More information about the Digitalmars-d-learn
mailing list