www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Global operator overloading?

reply Jeremie Pelletier <jeremiep gmail.com> writes:
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.
Aug 06 2009
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
Aug 06 2009
parent Jeremie Pelletier <jeremiep gmail.com> writes:
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.
Aug 06 2009