Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
flubbernugget

The D Programming Language

Recommended Posts

Just found out about the D programming language, and it looks pretty interesting. It's a lot like C++, but it adds new features and helps to get rid of many of the more confusing aspects of C++ like the scope operator. It also adds a lot of features seen in more high level languages like array slicing and foreach loops.Right now, it is somewhat tough to learn how to use because it is still an obscure language and has obscure documentation to complement it, but for experienced programmers the language should be pretty straightforward to learn.

What I really like about the language is that it can conform to the high level and low level needs of individual projects. For example, D has a garbage collector, but if need be it can be disabled for manual memory management and maximum efficiency.

The biggest problem I see with D is that it advocates variable declaration similar to that of Python where data types are implied instead of explicitly stated. Although it provides some write-time convenience, in the long run features like the auto keyword make figuring out the type of data when reading code harder than it should have to be. However, this is a fairly minor issue with D.

Has anybody else worked with D? What are your thoughts on it?

D's home page:

http://dlang.org/

Share this post


Link to post

I looked at it years ago and I didn't find it very compelling -- I don't remember exactly what put me off, perhaps because it tries to do too much.

Share this post


Link to post

It does nothing that can't be done by throwing enough add-ons and libraries to C/C++. Speaking of which, it's too much like C/C++ to really be as "easy" as Java or .NET, little incentive to use it as a primary development language since you'll need to interface with existing code or libraries in C or C++ anyway, and the results won't be backwards compatible or portable to other compilers, why not use C or C++ then?

Share this post


Link to post
Maes said:

It does nothing that can't be done by throwing enough add-ons and libraries to C/C++.

Maes said in a previous thread about source ports:
...aka the "but it compiles on my computer" scenario. Source code distribution != complete development environment.


So lets throw more libraries into C++ instead of implementing those features directly into a language so compilation is easier. Syntax is also less uniform with multiple libraries from different developers.

In all honesty though, as a new programmer I will admit that a big reason I decided to learn D was because of all the bad things I hear about C++, and since most of my programming is as a hobby I don't have to worry about industry standards.

Share this post


Link to post

I saw people advocating D a long time ago, but I had absolutely no clue people were still trying to use it. "For-each" loops have already been implemented into C++11 in the form of range-based for, and I honestly have never seen a situation where array slicing is a genuinely a must-have feature.

The scope operator isn't a difficult concept to understand either --- Use a period if you're trying to access an object's data, and the scope operator if you're referring to something that belongs to an enum, namespace, or class.

Share this post


Link to post

I see no advantage over C++ with Qt if you want a direct comparison to "C++ with enough libraries".

Share this post


Link to post
Belial said:

I see no advantage over C++ with Qt if you want a direct comparison to "C++ with enough libraries".


Not only that, but there are also the various "flavors" and "dialects" of C/C++ with vendor-specific extensions, convenience methods/libraries and the various RAD wizards to compete with e.g. Microsoft's C++ (with or w/o .NET, Borland C++ in the past etc. ), Microsoft's MFC libs, Borland Delphi's (still unmatched) drag & drop RAD etc.

Many modern C/C++ projects, especially those developed under Windows, are not as portable as one might think precisely because of that: too much reliance on the development environment, not just the language per-se. In such a scenario, it's a bit hard for a "bare bones" offering like D to find a niche (if you think about it, it could be implemented as syntactic sugar on top of existing C/C++ compilers, while not really novice-friendly enough as .NET or Java to justify using it as a stepping stone or viable alternative, let alone that there are no decent development tools for it unless you like very oldschool coding...)

Share this post


Link to post

It's not just Windows stuff that's not portable... A lot of non-trivial C/C++ Linux userland code just doesn't run on any other platforms without heavy modification. The Gnome and KDE ports in particular have been a big source of trouble for OpenBSD to keep up-to-date. I think some of that is related to the compiler itself, as gcc has made it rather difficult for the obsd devs to submit patches, because it's not considered to be a "first tier" OS or something like that. Effectively obsd devs have submitted patches to gcc so it can build and run correctly and they haven't gotten any feedback whatsoever, good, bad or indifferent from the gcc team for months on end. And when they finally do get feedback, the gcc codebase has changed enough to make the obsd patches obsolete. Rince, repeat, etc... I guess that's one reason they want to move away from gcc (at least to the point where the base system doesn't depend on it). But that won't make Gnome/KDE any easier to build. Not that I care, since I wouldn't be caught dead using such "desktop environment" suites, but those complex and bloated beasts are good examples of how non-portable C/C++ code can be.

Share this post


Link to post

Well then I guess my next question would be:

Why do I hear so much hate about C++ when no alternatives seem to be effectively proposed or implemented?

Share this post


Link to post
flubbernugget said:

Why do I hear so much hate about C++ when no alternatives seem to be effectively proposed or implemented?


Delphi? It offers C-level performance, can do all of C's low-lewel tricks, has objects, properties etc. and comes with a kickass RAD component-based development model, still unmatched by even the most advanced wizard Visual Studio can offer. BTW, the C# language was designed by the creator of Delphi...should say a lot ;-)

Share this post


Link to post

Now that I think about it, there's always Objective-C...a true Apple fanboy should code in nothing but that ;-)

Share this post


Link to post
Maes said:

Now that I think about it, there's always Objective-C...a true Apple fanboy should code in nothing but that ;-)

Especially if the low level code gets made only in C, no pluses.

Share this post


Link to post

Actually, it's all related: C++ can be viewed as a superset of C with A LOT of syntactic sugar and preprocessors added. Objective-C is actually designed to be a proper superset of C (moreso than C++).

Delphi/Pascal can be mapped almost 1:1 to C (there were even tools that did that automatically)...

Even more disturbingly, if you use a tool like GCJ, it actually compiles Java code into a C (or C++?) intermediate form...so in the end there's no escaping C, is there?

Even the way Doom is coded, the code "reeks" of OO influences from a mile away ;-)

Share this post


Link to post
flubbernugget said:

Why do I hear so much hate about C++ when no alternatives seem to be effectively proposed or implemented?

I would argue that it's mostly fud. Some old hats don't trust compiler optimizations (back in the day compilers weren't that great at this) and feel that everyone should hand optimize their code. That or they strongly believe that expensive operations should be difficult to write in order to make you think twice. There's also a lack of education in that most of OOP is just syntactic sugar, so some people worry that C++ will automatically slow down their program.

In reality, C++ is pretty much designed to not insert anything unless you explicitly call for it.

Share this post


Link to post

I've never cared for such opinions. It's a tool, everything depends on how you use it.

At my last job there were two guys who did C# exclusively (1 year and 3 years of professional experience). I've heard all the usual nonsense from them (lol pointers, unmanaged, memory leaks, outdated) until one of them was forced to do some C++/Qt stuff for Linux. A year later he used Qt for the next Windows desktop app he was supposed to write because he found it easier to do certain things with it. He'd still use C# if he had to do something server/database related, but for anything optimized for performance (video processing in this case) he'd stick to C++.

Pick the right tools for the goal ahead of you and don't give a damn about other peoples horror stories.

Share this post


Link to post

The funny thing is that both D and C# let you mix managed and "unsafe" code (though C# makes you jump through hoops whenever you do that). Even Java allows you to do it, through JNI and the infamous Unsafe class.

Share this post


Link to post

We're actually using D at work. Yes, an actual video game company is using D. A colleague of mine is well known on the D newsgroups as a result, but he recently left my workplace so now I'm taking over the mantle of D maintenance.

We had an internal game jam a few months back. My team wrote the game logic code in D. It was also the first time I'd programmed anything in the language. I read the first chapter of Andrei Alexandrescu's D book the night before the jam, and had my colleague on hand for any questions I had. I ended up programming with a more C# idiom than D, but I did manage to probe the language to see what it was capable of and made a construction table with one line of code (compared to the 1000-plus-line mammoth files we used to write on the PS2).

There's a lot of stuff I can go in to here, but as I'm at work I'll keep it to a minimum for now.

Share this post


Link to post
GooberMan said:

We're actually using D at work. Yes, an actual video game company is using D.


That puts you in a tactical position to make money writing some sort of "game programming in D" or whatever book, assuming there'd be little competition from competing writers, since you suggested few use D to make games, thus making you the only one selling something with that search term. I mean if you don't mind killing trees which is what selling dead tree copies is largely about.
You might think.. "that'd be a lot of work", but you're missing the point. Writing a book about programming isn't about teaching others to program at all (based on every programming book I've ever read). Its all about swindling the suckers who buy it to make money. Just shovel some fast food crap info into a book: "this is what a for loop is" etc. and use an extremely trivial pathetic "game", like tic tac toe or something, as the single recurring example throughout the book. You could write it in a day. Then use sock puppets to make fake positive reviews and fake negative reviews for any competing books. That's what your competition will be doing to you so its necessary to stay competitive.

Share this post


Link to post

Alright, so it's a double reply. Sue me.

D is quite a nice language to use. Its aim is to be a successor to C++ for low level systems programming. I think it's almost at the point where you could use it as such. In terms of speed, it's almost as fast as C++. There are many language-supported features that are done more efficiently in D than any C++ system could manage, but one major thing that holds it back speed wise is that every method of your classes are virtual by default. But it's a tradeoff with benefits there - D's type system is far more robust and safer than C/C++'s.

Arrays aren't just a pointer. They're a tuple of a pointer and a size. Going outside of the bounds of your array becomes far more difficult as a result. As a result of this, it's also got a fairly elegant solution for strings - as the string keyword is synonymous with immutable char[]. Any array operation is subsequently a valid string operation, including the ~ concatenate operator and slicing arrays for tokenising. It's also a fair sight cleaner than C#'s string implementation, which is the only basic type that behaves as a reference type rather than a value type.

I'm finding a heap of usage out of Universal Function Call Syntax. Basically, a function prototype of R function(T val) can be called in a similar manner to a property, ie val.function. It's not immediately obvious, but it straight up means you can extend the inbuilt types. floatVal.squareRoot for example is entirely possible. I wrote a WAD loader in D as an exercise, and I've found stuff like rotation.DoomRotationToRadians to be pretty handy. Combined with templates, it's a really freaking powerful feature to have.

Speaking of. Templates are far less of a headache than C++. Specialisation is fully supported, so you can restrict your template to certain types or other qualifiers.

There's other cool things too, such as opDispatch. Have you ever wanted to swizzle a vector in C++ code? opDispatch lets you interpret any old accessor string on your classes/structs and decide what to do with it. vector.xzy is completely valid if you've created the correct opDispatch function.

By far the most valuable thing though in D is that the preprocessor has been eliminated. There's still conditional compilation via things like the version keyword (which handles processor architectures, unit testing, etc). static if is also quite valuable in that regard - it basically means that swizzle operator I mentioned above can be compiled away to nothing. But by far the best part with the most ramifications for your coding style is the fact that compile-time code execution is implemented. Any D code you write can be evaluated and run at compile time and thus affect the output of your code. It's meta programming on a whole new level. The kind of stuff you can do it with is nuts. We're using it extensively at the moment, I can't really go in to details yet. Needless to say, it makes #define look as archaic as it actually is, and other metaprogramming solutions weak in comparison. The amount of code it cleans up and effort it saves over the corresponding C++ equivalent is insane. And since you're evaluating stuff at compile time, massive C++ systems can effectively be compiled out to nothing in D.

As D fully supports the C ABI, it can also bind all the commonly used libraries (Phobos in fact has bindings to the C runtime functions). It also has limited compataibility with C++. There isn't a single consistent C++ ABI out there, so D has limited itself to supporting the C++ virtual function table. If you write matching interfaces in C++ and D, you can cross-communicate between the two with your objects.

I'm not an expert on D yet, but I'll be getting more knowledgable and confident in the language both out of necessity and by choice.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×