Why isn't global operator overloading allowed in D?

WagonPassenger via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Oct 14 21:45:22 PDT 2015


On Wednesday, 14 October 2015 at 15:02:02 UTC, Shriramana Sharma 
wrote:
> Hello. I just came upon a need in my program to make binary 
> arithmetic operators valid between two real[] in my programs, 
> and thought of writing a global opOpAssign, but then looked 
> through the documentation, found nothing on operator 
> overloading allowed at the global level (even within a single 
> module), and searched through the forum and saw this thread: 
> http://forum.dlang.org/post/jeyaozvegcnivcppucpo@forum.dlang.org
>
> Why isn't global operator overloading allowed in D?
>
> Having to construct a class just to write a quick operator 
> overload for two built-in types isn't a clean solution, so 
> please don't suggest that (like it was suggested in the other 
> thread).
>
> Thanks.
>
> Shriramana Sharma.

Is Writing a global function an option ? With UFCS it will still 
look correct...
And under the hood, an operator overload is a function anyway.
With string template parameter it could even display the symbol:

(not tested but you shoud get the idea)
---
void assignResultOf(string op, L, R)(ref L lhs, ref R rhs) {}

assignResultOf!"+"'(dest, src);
dest.assignResultOf!"+"'(src);
---

otherwise struct + alias this + member operator overload is the 
most obvious way to overcome the problem...but it seems you don't 
like this.






More information about the Digitalmars-d-learn mailing list