so:
> With this in mind, just one thing bugs me.
> 
> ----
> import std.stdio;
> 
> struct vector(T) {
>       this(T m) { mm = m; }
>       vector!T opBinary(string s)(T m) if(s=="*") {
>               return vector!T(mm * m);
>       }
>       T mm;
> }
> 
> void test(T)() {
>       vector!T v = vector!T(0.5);
>       vector!T u = v * 0.3;
>       writeln("u: ", u.mm);
> }
> 
> void main() {
>       test!float();
>       test!double();
>       test!real();
> }
> -----
> 
> This program compiles and runs just fine, but i feel dirty, you see I  
> explicitly stated that opBinary takes a variable of type T,
> but it accepted 0.3 on all 3 tests, implicitly casted double to T. In C++  
> world this brings tons of trouble, especially performance problems,
> but here i am not sure what DMD does that there :)

Are you trying to do this?

import std.stdio: writeln;

struct Vector(T) {
    this(T m) { mm = m; }
    Vector opBinary(string op:"*", T2)(T2 m) if(is(T2 == T)) {
        return Vector(mm * m);
    }
    T mm;
}

void test(T)() {
    Vector!T v = Vector!T(0.5);
    Vector!T u = v * 0.3;
    writeln("u: ", u.mm);
}

void main() {
    //test!float();
    test!double();
    //test!real();
}

Bye,
bearophile

Reply via email to