generic + numeric + literals = abomination

bearophile bearophileHUGS at lycos.com
Sun Mar 28 03:32:21 PDT 2010


so:

> Well, i am having hard time explaining, it is not a surprise that you  
> don't understand.

I think I have understood you this time. Writing skills are important for a programmer :-)
Is this what you are asking for? But it's not very good code:

import std.stdio: writeln;

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

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

void main() {
     test!ubyte();
     test!ushort();
     test!uint();
     test!ulong();
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list