PROPOSAL: Operator overloading by static member function

Janice Caron caron800 at googlemail.com
Sun Oct 14 00:50:58 PDT 2007


On 10/14/07, Daniel Keep <daniel.keep.lists at gmail.com> wrote:
> Actually, I believe someone's done something like this for D before.
> Their goal, however, was *compile-time* checking of units.  So, for
> example, you couldn't do this:
>
> auto v = 3*m/s;
> auto a = 9.81*m/(s*s);
> auto huh = v+a;

Well yeah. That's what I'd want - compile time dimension checking. And
it seems reasonably achievable with your system. And not to mention
unit conversion, eg.

auto d = 15*miles + 20*km;
writefln(d.cm,"cm"); /* prints d in centimeters */

Of course, you'd really want it to be clever enough to understand when
the units cancel out, e.g.

auto n = (3*s) * (30*Hertz); /* n is real */

Could be we're looking for some sort of template struct, eg.

struct SIUnit(int m,int s,int kg)
{
    real r;
}

SIUnit!(1,0,0) x; // meters
SIUnit!(1,-2,0) y; // meters per second per second
SIUnit!(2,-2,1) z; // joules;

Hmm...



More information about the Digitalmars-d mailing list