On 08/30/2015 07:02 PM, Spacen Jasset wrote:
I have just added an opDiv to this class, but it doesn't seem to pick it
up.
math/vector.d(30): Error: 'this /= mag' is not a scalar, it is a Vector3

I can't see why that is, becuase my opMul works in the same place. Can
anyone point out what I have done wrong?
...

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`);
    }
}

Reply via email to