Computer Science Canada [D] Major Points Of Note For C++ Programmers |
Author: | wtd [ Sat Oct 30, 2004 3:59 am ] | ||||
Post subject: | [D] Major Points Of Note For C++ Programmers | ||||
Forward declarations are unnecessary The D compiler evaluates the entire source file, so forward declarations of functions are never needed. C++
D
More to come. |
Author: | Andy [ Sat Oct 30, 2004 9:49 am ] |
Post subject: | |
cool.. no need for prototypes... |
Author: | Tony [ Sat Oct 30, 2004 2:53 pm ] |
Post subject: | |
ohh.. I'm liking D thus far.. what else does it have to offer? |
Author: | wtd [ Sat Oct 30, 2004 4:38 pm ] | ||||||
Post subject: | |||||||
Say goodbye to #include D adopts the Java-ish "import" syntax, but puts an even better module system behind it. Let's say I have two source files: foo.d:
And my main file, test.d:
And I compile and run like so:
Now you're probably first wondering about that "private". It means that essentially, even though I've imported the std.stream module into the foo module, anything that imports foo shouldn't automatically import std.stream. Now, did I do anything to indicate that foo.d was a module? Nope. Any file is automatically a module that can be reused. Just take off the file extension and you're set to go. Update: the DigitalMars D compiler doesn't seem to enforce private imports yet. Oh well. Still an improvement. |
Author: | wtd [ Sat Oct 30, 2004 5:34 pm ] | ||||||||
Post subject: | |||||||||
Function parameters C++ supports passing variables to functions by reference, but it uses the & operator, which can get lost in a function declaration. D improves on this by introducing three new keywords: in, out, and inout. Consider the following trivial example: C++
D
The "in" version is used by default. "inout" is used when you want to both read the value in a variable and write to it. C++
D
Of course, whatyou should be picking up from these examples, is that D in many ways improves on C++, but it's also not that different, either. |
Author: | wtd [ Sat Oct 30, 2004 6:39 pm ] | ||||||||||
Post subject: | |||||||||||
Design by Contract: Pre and Post Conditions, Classes, and Properties, oh my! Let's say we have a "NameBase" class. Yes, this is one of my favorte trivial examples. C++
D
Now, we'll extend it so that we can get access to the parts of the person's name, and also change them. C++
D
So, there's properties demonstrated, and some nifty bits about classes, including constructors. So, we're left with pre and post conditions. Let's play with inheritance and create NotStupidName class which basically puts pre and post conditions on our ability to set the first and last names. People with the same firs and last names sound kinda stupid, so we'll prevent that.
|
Author: | wtd [ Sat Oct 30, 2004 10:05 pm ] | ||||||
Post subject: | |||||||
Back to little things Time for a break after that last post. The foreach loop Much like the most recent version of Java and C#, D provides a convenient way to loop over aggregate data structures like arrays.
Of course, we can use the foreach loop to modify the contents of an array too.
Did I mention that strings are just arrays of characters? That means we can use strings in foreach loops too.
|
Author: | wtd [ Sat Oct 30, 2004 10:57 pm ] | ||
Post subject: | |||
Switch works on strings
|
Author: | rizzix [ Sat Oct 30, 2004 11:13 pm ] |
Post subject: | |
i like the consistancy in D.. although i prefer strings as objects than characters... nywyz c++ should've really been like D.. its the c++ that is not backward compatible with a cleaned up syntax.. too bad it does not have much support yet. apple should've gone for D than obj-c,, and it's easier to map D to Java. (not implying that obj-c is any less good.. it has it's own strengths) |
Author: | wtd [ Sat Oct 30, 2004 11:32 pm ] |
Post subject: | |
Well, D didn't exist at the time (1997), and NeXT already had a very mature, extensive framework written in Objective-C. Besides, Objective-C is vastly more dynamic than C++, Java or D. Very few languages could do the same job. As for strings... strings are objects in Java because... well anything that isn't a integral type or float is an object. In C++ it's to make up for the fact that arrays in C++ are just glorified pointers. Arrays in D are more sophisticated, so it's quite acceptable that strings aren't objects. |
Author: | rizzix [ Sat Oct 30, 2004 11:35 pm ] |
Post subject: | |
do arrays in D hold length information? edit: nvm they do... nice and there's support for slicing too.. sweet. but that is as far as it goes.. oh and they have the cleaner syntax for Dictionaries. |
Author: | wtd [ Sun Oct 31, 2004 12:45 am ] | ||||
Post subject: | |||||
Debugging simplified test.d
Compiling it:
|