dmd 1.057 and 2.041 release

bearophile bearophileHUGS at lycos.com
Mon Mar 8 14:03:50 PST 2010


> 2) What's the best way to translate this to the new operator regime?
> 
> T foo(T)(T s) if (__traits(hasMember, T, "opAdd")) {
>     return s + s;
> }

I have not found a good solution yet. This solution looks buggy (also because fixed-sized arrays have a buggy .init):


import std.stdio: writeln;

struct Foo {
    int x;

    this(int xx) { this.x = xx; }

    Foo opBinary(string s:"+")(Foo other) {
        return Foo(this.x * other.x);
    }
}

T foo(T)(T s) if (__traits(compiles, {return T.init + T.init;})) {
    return s + s; // line 14
}

void main() {
    auto f1 = Foo(2);
    auto f2 = Foo(3);
    writeln(f1 + f2);
    writeln(foo(f1));

    int[2] a = [1, 2];
    writeln(typeid(typeof(a.init))); // prints: int
    writeln(foo(a)); // test.d(14): Error: Array operation s + s not implemented
}


Bye,
bearophile


More information about the Digitalmars-d-announce mailing list