· D vs C/C++/C#/Java · Rationale for Builtins · Converting C to D · Converting C++ to D · The C Preprocessor vs D · D strings vs C++ std::string · D complex vs C++ std::complex · D Contract Programming vs C++ |
Core Language Features vs Library ImplementationD offers several capabilities built in to the core language that are implemented as libraries in other languages such as C++:
Some general initial observations:
Dynamic ArraysC++ has builtin core arrays. It's just that they don't work very well. Rather than fix them, several different array types were created as part of the C++ Standard Template Library, each covering a different deficiency in the builtin arrays. These include:
As usual, a builtin type lets us create syntactic sugar for it. This starts with having an array literal, and follows with some new operators specific to arrays. A library array implementation has to make due with overloading existing operators. The indexing operator, a[i], it shares with C++. Added are the array concatenation operator ~, array append operator ~=, array slice operator a[i..j], and the array vector operator a[]. The ~ and ~= concatenation operators resolve a problem that comes up when only existing operators can be overloaded. Usually, + is pressed into service as concatenation for library array implementations. But that winds up precluding having + mean array vector addition. Furthermore, concatenation has nothing in common with addition, and using the same operator for both is confusing. StringsA detailed comparison with C++'s std::string. C++ has, of course, builtin string support in the form of string literals and char arrays. It's just that they suffer from all the weaknesses of C++ builtin arrays.But after all, what is a string if not an array of characters? If the builtin array problems are fixed, doesn't that resolve the string problems as well? It does. It seems odd at first that D doesn't have a string class, but since manipulating strings is nothing more than manipulating arrays of characters, if arrays work, there's nothing a class adds to it. Furthermore, the oddities resulting from builtin string literals not being of the same type as the library string class type go away. Associative ArraysThe main benefit for this is, once again, syntactic sugar. An associative array keying off of a type T and storing an int value is naturally written as:int[T] foo;
rather than:
import std.associativeArray; ... std.associativeArray.AA!(T, int) foo;Builtin associative arrays also offer the possibility of having associative array literals, which are an often requested additional feature. Complex NumbersA detailed comparison with C++'s std::complex.The most compelling reason is compatibility with C's imaginary and complex floating point types. Next, is the ability to have imaginary floating point literals. Isn't: c = (6 + 2i - 1 + 3i) / 3i;far preferable than writing: c = (complex!(double)(6,2) + complex!(double)(-1,3)) / complex!(double)(0,3);? It's no contest. |