d重载操作符示例

原文
重载路径:

struct Path {
    string value;
    Path opBinary(string op)(in string path) if(op == "/") {
        import std.path : buildPath;
        return Path(buildPath(value, path));
    }
}

void main() {
    assert(Path("/foo") / "bar" == Path("/foo/bar"));
}

继续:

version (Windows) {
    assert(Path("C:\Users") / "Paul" == Path("C:\Users\Paul"));
}

最好只使用~连接
另外,如果除法乘法,是否:

Path("/foo/bar") * "bar" == Path("/foo")

?

posted @ 2022-10-25 09:26  zjh6  阅读(10)  评论(0)    收藏  举报  来源