digitalmars.D.learn - Why isn't global operator overloading allowed in D?
- Shriramana Sharma (12/12) Oct 14 2015 Hello. I just came upon a need in my program to make binary arithmetic
- Shriramana Sharma (7/15) Oct 14 2015 Hmm, just now saw this one too:
- John Colvin (4/6) Oct 14 2015 What binary arithmetic operators do you need that real[] doesn't
- Shriramana Sharma (2/6) Oct 15 2015 OMG silly me! I can already do a[] /= b[]... D is great! :-D Thanks a lo...
- John Colvin (13/22) Oct 15 2015 Also:
- WagonPassenger (15/29) Oct 14 2015 Is Writing a global function an option ? With UFCS it will still
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
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
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 programsWhat binary arithmetic operators do you need that real[] doesn't already support?
Oct 14 2015
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
On Thursday, 15 October 2015 at 15:45:00 UTC, Shriramana Sharma wrote:John Colvin wrote: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.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
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