Global operator overloading?

Jeremie Pelletier jeremiep at gmail.com
Thu Aug 6 19:06:50 PDT 2009


Andrei Alexandrescu Wrote:

> Jeremie Pelletier wrote:
> > Are there any plans to have global operator overloading in D?
> > 
> > It would be terribly useful with ranges, since std.array already define range primitives for arrays, I tried to implement array primitives to ranges the same way, ie:
> > 
> > ---
> > bool opEquals(T, U)(T t, U, u) if(isInputRange!T && isInputRange!U) {
> > 	return equal(t, u);
> > }
> > ---
> > 
> > so I wouldnt have to change every a==b to equal(a, b) when using ranges where built-in arrays were previously used.
> > 
> > Of course if theres another way to do it (other than manually adding opEquals and others to every range declaration in phobos), feel free to suggest it.
> 
> I guess the compiler could rewrite a == b into a.opEquals(b) and then 
> let normal rules find opEquals(a, b). The latter could sit in object.d.
> 
> Andrei

Yeah, global operators overloads would be at the very end of the lookup chain, and built-in types ignored too. So only custom aggregates would be able to match these.

Anywho, I've fixed my issue by adding a template mixin to most ranges in my local phobos, but I would much prefer a more generic solution.



More information about the Digitalmars-d mailing list