Can i rewrite methods in one line?

Ivan Agafonov armadil at yandex.ru
Sun Sep 2 19:57:48 PDT 2012


On Monday, 3 September 2012 at 02:40:09 UTC, Ali Çehreli wrote:
> 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

I made a mistake here. This operators have already "onelined" :

ref Vector opAssign(Vector rhs) { array[] = rhs.array[]; return 
this; }
ref Vector opAssign(T[size] rhs) { array[] = rhs[]; return this; }
	
ref Vector opOpAssign(string op) (T[size] rhs) if(op == "+" || op 
== "-" || op == "*" || op == "/")
{ mixin("array[] "~op~"= rhs[];"); return this; }
	
ref Vector opOpAssign(string op) (Vector rhs) if(op == "+" || op 
== "-" || op == "*" || op == "/")
{ mixin("array[] "~op~"= rhs.array[];"); return this; }
	
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[]"));
because array[] op= rhs.array[] is not a right expression it does 
not return an array, it can only be used in assigment to slice 
like this[]. And expression array op= rhs.array does not make 
sense too.



More information about the Digitalmars-d-learn mailing list