Operator overloading -- lets collect some use cases

Don nospam at nospam.com
Thu Jan 1 04:36:36 PST 2009


Daniel Keep wrote:
> 
> 
> 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...

My BLADE library pretty much does that, so you don't need to guess.
The ast is a string of the form "A=B+(C*D)", together with a type tuple 
Tuple!(Vector, Vector, double, Vector), and a values array 
["a","b","(g*h)","c"].
("g*h" is something like "4.564e+2" if g and h are compile-time constants).
One approach would be to look at that code and try to simplify it.

> 
> 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