Operator overloading -- lets collect some use cases

Daniel Keep daniel.keep.lists at gmail.com
Wed Dec 31 23:13:43 PST 2008



Andrei Alexandrescu wrote:
> [snip]
> 
> All right, let's do this with operator overloading :o). Ideas? I have a 
> couple, but I don't want to introduce bias.
> 
> Andrei

The only idea I can come up with is to introduce the following:

struct Vector
{
     macro opExpression(ast)
     {
         ...
     }
}

The compile-time opExpression macro's job is to spit out a function that 
takes each leaf of the AST as an argument and returns the final value of 
the expression.

You could simplify it by ensuring that you don't get any subexpressions 
that don't result in the type "Vector".  For example:

Vector a, b, c;
double g, h;

a = b + (g*h) * c;

The AST would have "(g*h)" as a single node of type double.

Of course, this would probably be exceedingly complex, both on the 
compiler and user side of things.  I can't imagine what the contents of 
that macro would even look like...

The one positive to this method is that it's about as general as you can 
get; assuming this was implemented, it would hopefully be possible to 
implement a simpler scheme on top of it.

struct Vector
{
     mixin FusionOverloadFor!("+","-","*","/",".dot",".cross");
}

I really hope there's a better way.

   -- Daniel



More information about the Digitalmars-d mailing list