www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Why isn't global operator overloading allowed in D?

reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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.
Oct 14 2015
next sibling parent Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
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?
Hmm, just now saw this one too: http://forum.dlang.org/post/l7nnnb$1ma0$1 digitalmars.com -- posted that too hastily. But really, it's too bad built-in types are different from user-made types unlike in Python. In Python, I can inherit from list and dict and whatnot (yes I have used them in real apps)...
Oct 14 2015
prev sibling next sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
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
What binary arithmetic operators do you need that real[] doesn't already support?
Oct 14 2015
parent reply Shriramana Sharma <samjnaa_dont_spam_me gmail.com> writes:
John Colvin wrote:

 On Wednesday, 14 October 2015 at 15:02:02 UTC, Shriramana Sharma
 wrote:
 What binary arithmetic operators do you need that real[] doesn't
 already support?
OMG silly me! I can already do a[] /= b[]... D is great! :-D Thanks a lot!
Oct 15 2015
parent John Colvin <john.loughran.colvin gmail.com> writes:
On Thursday, 15 October 2015 at 15:45:00 UTC, Shriramana Sharma 
wrote:
 John Colvin wrote:

 On Wednesday, 14 October 2015 at 15:02:02 UTC, Shriramana 
 Sharma
 wrote:
 What binary arithmetic operators do you need that real[] 
 doesn't
 already support?
OMG silly me! I can already do a[] /= b[]... D is great! :-D Thanks a lot!
Also: a[] = b[] + c[] * d[] - 42; and so on... All that's required is that there is pre-allocated memory for the result to go in i.e. there has to be enough space in a[]. You should be aware that with DMD these array operations should be much faster than a straightforward loop, as they are done in handwritten asm using vector instructions. Be wary of using them on very small arrays, there is some overhead. With LDC/GDC you probably wont see much difference either way, if I remember correctly they rely on the optimiser instead.
Oct 15 2015
prev sibling parent WagonPassenger <WagonPassenger nowhere.ut> writes:
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.
Oct 14 2015