stuck on opDiv / opBinary

Spacen Jasset via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Aug 30 16:09:33 PDT 2015


On Sunday, 30 August 2015 at 20:09:25 UTC, Timon Gehr wrote:
> On 08/30/2015 07:02 PM, Spacen Jasset wrote:
>> [...]
>
> import std.math: sqrt;
> import std.algorithm: map,sum,canFind;
>
> struct Vector3{
>     float[3] xyz;
>     void normalise(){ this/=magnitude();  }
>     float magnitude(){ return sqrt(xyz[].map!(a=>a*a).sum); }
>     enum scalarOps=["*","/"];
>     enum isScalarOp(string op)=scalarOps.canFind(op);
>     void scalar(string op)(float scalar)if(isScalarOp!op){
>         foreach(ref a;xyz) mixin(`a `~op~`=scalar;`);
>     }
>     Vector3 opBinary(string op)(float scalar)if(isScalarOp!op){
>         Vector3 v=this;
>         v.scalar!op(scalar);
>         return v;
>     }
>     auto opOpAssign(string op)(float rhs)if(isScalarOp!op){
>         return mixin(`this=this `~op~` rhs`);
>     }
> }

Thanks, that is interesting. I am curently porting, rather than 
trying to rewrite anything at the moment, but I will try this out 
later.


More information about the Digitalmars-d-learn mailing list