I came from C++ looking forward to D. Some languages require programmers to use GC all the time. However, A lot of time we don't really need GC especially when the time of destruction is deterministic in compile time.

I found that struct in D is allocate on stack by default. And we can use scope! to allocate class on stack too.

See the following code. Struct version of Vector3f can't derive toString method. writeln() prints unformated struct members. I know I can use helper function here. But is there any other way?

struct Vector3f {
public:
    this(float x, float y, float z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    @property float length2() {
        return x*x+y*y+z*z;
    }

    @property float length() {
        import std.math;
        return sqrt(x*x+y*y+z*z);
    }

    Vector3f opBinary(string op)(Vector3f rhs)
    {
static if (op == "+") return Vector3f(x+rhs.x, y+rhs.y, z+rhs.z); else static if (op == "-") return Vector3f(x-rhs.x, y-rhs.y, z-rhs.z);
        else static assert(0, "Operator "~op~" not implemented");
    }

    float x, y, z;
}

class version of Vector3f. Require new operator in opBinary(). scoped! won't work here.

Is there a better to write vector3f class while avoiding GC?

Reply via email to