GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing dlang.org — Rationale - D Programming Language Articles
Report a bug
If you spot a problem with this page, click here to create a GitHub issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
a local clone.
Rationale Contents
Questions about the reasons for various design decisions for D often come up. This addresses many of them. Operator OverloadingWhy not name them operator+(), operator*(), etc.?This is the way C++ does it, and it is appealing to be able to refer to overloading ‘+’ with ‘operator+’. The trouble is things don't quite fit. For example, there are the comparison operators <, <=, >, and >=. In C++, all four must be overloaded to get complete coverage. In D, only an opCmp() function must be defined, and the comparison operations are derived from that by semantic analysis. Further, binary operators on number-based types are largely uniformly implemented so a single opBinary template allows one to just mixin the operator the user used, whereas in C++ each operator will need to be separately defined. For example: import std.stdio; struct MyInt { int i; MyInt opBinary(string op)(in MyInt other) if(op == "+" || op == "-") { mixin ("return MyInt(i " ~ op ~ "other.i);"); } } void main() { MyInt a = MyInt(3), b = MyInt(1), c = a + b, d = a - b; writeln(a.i, ' ', b.i, ' ', c.i, ' ', d.i); // prints 3 1 4 2 } Overloading operator/() also provides no symmetric way, as a member function, to overload the reverse operation. For example, class A { int operator/(int i); // overloads (a/i) static operator/(int i, A a) // overloads (i/a) } The second overload does the reverse overload, but it cannot be virtual, and so has a confusing asymmetry with the first overload. Why not allow globally defined operator overload functions?
Why not allow user definable operators?These can be very useful for attaching new infix operations to various unicode symbols. The trouble is that in D, the tokens are supposed to be completely independent of the semantic analysis. User definable operators would break that. Why not allow user definable operator precedence?The trouble is this affects the syntax analysis, and the syntax analysis is supposed to be completely independent of the semantic analysis in D. Why not use operator names like __add__ and __div__ instead of opAdd, opDiv, etc.?__ keywords should indicate a proprietary language extension, not a basic part of the language. Why not have binary operator overloads be static members, so both arguments are specified, and there no longer is any issue with the reverse operations?This means that the operator overload cannot be virtual, and so likely would be implemented as a shell around another virtual function to do the real work. This will wind up looking like an ugly hack. Secondly, the opCmp() function is already an operator overload in Object, it needs to be virtual for several reasons, and making it asymmetric with the way other operator overloads are done is unnecessary confusion. PropertiesWhy does D have properties like T.infinity in the core language to give the infinity of a floating point type, rather than doing it in a library like C++: std::numeric_limits<T>::infinity ?Let's rephrase that as “if there's a way to express it in the existing language, why build it in to the core language?” In regards to T.infinity:
To sum up, there's a lot more to supporting NaNs and infinities than having a template that returns a bit pattern. It has to be built in to the compiler's core logic, and it has to permeate all the library code that deals with floating point. And it has to be in the Standard. To illustrate, if either op1 or op2 or both are NaN, then: (op1 < op2) does not yield the same result as: !(op1 >= op2) if the NaNs are done correctly. Why use static if(0) rather than if (0)?Some limitations are:
Copyright © 1999-2026 by the D Language Foundation | Page generated by
Ddoc on Mon Nov 9 05:59:21 2026
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |