在 D 中的进出合同之间传递值

Passing values between in and out contracts in D

我是 D 的新手。我开始使用合同,我认为它们很棒。但是,我似乎无法在文档中找到我可以像这样存储合同可用的本地值...

struct Vector2(T) {
    T x = 0;
    T y = 0;

    Vector2!T opBinary(string op)(const ref Vector2!T rhs)
        if(op == "+" || op == "-" || op == "*" || op == "/")
    in {
        T prevx = this.x;
        T prevy = this.y;
    }
    out {
        static if(isFloatingPoint!T) {
            assert(mixin("approxEqual(this.x, prevx"~op~"rhs.x)"));
            assert(mixin("approxEqual(this.y, prevy"~op~"rhs.y)"));
        } else {
            assert(mixin("this.x == (prevx"~op~"rhs.x)"));
            assert(mixin("this.y == (prevy"~op~"rhs.y)"));
        }
    }
    body {
        Vector2!T ret;
        mixin("ret.x = this.x"~op~"rhs.x;");
        mixin("ret.y = this.y"~op~"rhs.y;");
        return ret;
    }
}

我需要在执行正文之前 x 和 y 的值,以便我可以正确验证 out 块中的结果。但是,prevx 和 prevy 显然不能在范围之间移动。是否有某种方法可以在进出合同之间传递数据?

唉,没有。你能做的最好的事情就是将它存储在外部的一些其他变量中,你可以在那里遇到递归等问题:(