dmd 1.057 and 2.041 release

bearophile bearophileHUGS at lycos.com
Mon Mar 8 14:53:31 PST 2010


Robert Clipsham:
> Untested, will the following do what you need?
> ----
> T foo(T)(T s) if (__traits(compiles, {return s + s;})) {
>       return s + s;
> }
> ----
> Seems like you may as well test if you can add what you're passed rather 
> than the initial value for the type.

Oh, nice, I didn't remember you can also use values there and not just types. It becomes like this then:


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 s + s;})) {
    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
}


But now I don't know what's happening, because that trait correctly returns false, but the compiler generates a compile error at line 14 still. I think there's a new bug.

Bye,
bearophile


More information about the Digitalmars-d-announce mailing list