PROPOSAL: Operator overloading by static member function

Reiner Pope some at address.com
Sun Oct 14 01:29:23 PDT 2007


Janice Caron wrote:
> 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...

Oskar Linde wrote one already: http://www.csc.kth.se/~ol/physical.d

But you don't actually need static opMul for that. To quote two lines of it:

const Length 		meter 		= {1};
...
Length l = 0.1 * meter;

As you can see, it uses instance opMul.

But still very cool.

    -- Reiner



More information about the Digitalmars-d mailing list