Hello World in D!

Recently I came across D programming language. It is the successor of c++ with some Java like features. Since I am a C/C++ lover, I have a stupid feeling when I start coding with either Java or C#. I still will give a try and see how many points does it gain on my scale. Though I haven’t seen any widespread use of D. D was invented by Walter Bright of DigitalMars in December 1999 (http://www.prowiki.org/wiki4d/wiki.cgi?WhatIsD). Some of the features of D as it says on its webpage (http://www.digitalmars.com/d/2.0/overview.html) are resizable arrays, garbage collector, etc besides providing low level bare metal access as required. It also boasts of having a very optimizing and generally fast compiler as compared to C++ as many features which took a lot of compiler time for parsing source files have been removed. Interesting part is that some features like #include preprocessors, #defines, etc. are dropped. The overview page has a lot more text and interested people can go and visit it. Also says that it produces files of smaller size.

So all the people who want to skip the bla bla and deep dive into D, first things to do is to download the digialmars D compiler dmc 2.0.6 (an alpha version) as of now but works fine. I use code::blocks as an IDE which provides a template for D (which is an initial version of Tango so won’t work anyways). But go ahead and use Code::Blocks. It is the simplest, best IDE and believe me, I have used a lot of them. Go ahead and select a new D project. A wizard will popup and for the time being you can use HelloWorldD as the project name and select a location to create the project in. Click on next/finish and you get a HelloWorld.D file created. Goto settings->Compilers & Debuggers and select the location of the bin directory where you have extracted the D compiler and linker. Also give the lib path properly. Okay everything and press CTRL+F9. It will give errors. So delete all the stuff that is present in .D file and write the following.

import std.stdio;
void main()
     writefln("Hello World with D Language!");

Press CTLR+F9 and then CTRL+F10 and it should run fine. For people who know C/C++, printf/cout is replaced with writefln(); I don’t know what writefln stands for (probably write fucking line or write functional line or write line to filestream with filestream redirected to standard out, choose whatever you want). Anyways, probably there is something in the documentation that tells you what it is. I am a hands on guy and not much of a reader of documents. So lets create a simple C++ program and see sizes, compile times, etc. when compared to a D program.

I will divide this post into multiple parts so that I don’t bore people reading it with one long unreadable post. But proceed to the other post titled Part 2 which has the C++ code for a classic class Complex with some operator overloading. Lets see how it fairs in C++ and D.