Remove complex and imaginary types?
Daniel919
Daniel919 at web.de
Sat Jan 12 03:45:19 PST 2008
> Why not the asterisk? Oh wait! - We already /have/ that functionality!
>
> 5.8f*km
>
> is replaced by
>
> km.opMul_r(5.8f);
>
> Seems to me that nothing new is needed.
Here are some ideas how it would be even better than it could be with
opSuff.
(opSuff would not allow: "z=5i;" <=> "z=5*i;")
-----------------------------------------------
import std.stdio, std.conv;
struct complex {
real re;
real im;
complex opAdd(complex z) { return complex(re+z.re, im+z.im); }
complex opAdd(real r) { return opAdd( complex(r,0) ); }
//alias opAdd(real r) opAdd_r(real r);
complex opAdd_r(real r) { return opAdd( r ); }
complex opMul(complex z) { return complex(re*z.re-im*z.im,
re*z.im+im*z.re); }
complex opMul(real r) { return opMul( complex(r,0) ); }
//alias opMul(real r) opMul_r(real r);
complex opMul_r(real r) { return opMul( r ); }
string toString() { return std.conv.toString(re) ~ "+" ~
std.conv.toString(im) ~ "i"; }
struct i {
static complex opMul_r(real im) { return complex(0,im); } //z=5*i;
// static opJux_r(real im) { return complex(0,im); } //z=5i; //z=5f i;
//z=5fi; is tricky, especially if "fi" is a symbol
//but if "with(complex)" is used, "i" dominates over "fi", so this
becomes: z=complex.i.opJux_r(5f);
// static opExpr() { return complex(0,1); } //z=i;
//is considered if none of the functions above matches
//it would even be possible to make opExpr() the only function of i
}
}
void main() {
complex z; with(complex) z = 1+5*i + 2+3*i + 6*i; //maybe
"with(complex)" can be inferred, when creating a complex?
// complex z = 1+complex(0,5) + 2+complex(0,3) + complex(0,6);
writefln( z );
}
-----------------------------------------------
More information about the Digitalmars-d
mailing list