PROPOSAL: Operator overloading by static member function

Reiner Pope some at address.com
Sun Oct 14 02:31:25 PDT 2007


Janice Caron wrote:
> On 10/14/07, Reiner Pope <some at address.com> wrote:
>> But you don't actually need static opMul for that. To quote two lines of it:
> 
> That's true. Put like that, static opAdd(), static opSub(), etc., do
> start to sound a bit silly. I can't see why you would ever need to
> write:
> 
> x = A + y;
> 
> where A was a type. Surely, any time you can imagine a use for that,
> you could probably do the same thing with A just being a variable?
> 
> (The only one I can think of that has any real use is static opCall()
> -- and even that need would disappear if structs had constructors)

Well, here's a ... good ... use:

struct foo
{
     static bool opMul(foo* x)
     { return true;}
}

void main()
{
     foo* p;
     assert(foo* p); // just checking that we declared p correctly

     foo* null;       // hey, look! null really can be an identifier
}

Of course, you could do most of it with foo being a variable, but it's 
not anywhere near as expressive. :D

   -- Reiner



More information about the Digitalmars-d mailing list