Perhaps you need the D

Perhaps you need the D

By | August 10th, 2015
No Comments on Perhaps you need the D

It’s quite similar to C++ but the world is torn over using D rather than

the former


														
							

Major Design Goals of D

Every language has certain design goals that are the guidelines for the development of the language and here are some of the design principles for D.

1. Make writing code easier that is portable from compiler to compiler, machine to machine and from operating system to operating system.
2. To provide syntactic and semantic constructs to eliminate common programming mistakes.
3. Support memory safe programming.
4. Have a shorter learning curve for programmers comfortable with C or C++ or Java.
5. Be compatible with the binary interface of the local C application
6. Have context free grammar
7. Incorporate contract programming and unit testing
8. Help build lightweight and standalone programs
9. Reduce documentation creation costs

Who should use D?

Programmers who regularly use lint or similar code analysis tools before the code is even compiled. Those who keep warning levels at maximum with warning categorised as errors. For projects that consist of million lines of code that need built-in testing and verification.

Then there are numerical programmers, since D supports features like floating point precision, core support for floating types and defined behaviours for NaN’s and infinities.

progr

D programming with Code::Blocks as the IDE

D’s compiler structure is modular. The lexical analyser and parser are totally independent of each other and the semantic analyser so if you were to write a simple tool to manipulate the source code then you can modify parts of the compiler rather than the entire compiler. This also means the source code can be broken down as tokens for specialised applications. And lastly, for those who believe that C++ is just not enough a little modern touch is necessary for their project. The language as a whole is kept as close as possible to C++ which means a transition into D shouldn’t be difficult at all.

Who shouldn’t use D?

Let’s face it, C++ has matured over the ages so there is no way someone is going to sit down and convert his/her million line C/C++ program into D. D is similar to C/C++ but it doesn’t compile C/C++ source code out of the box. However, D supports legacy C API’s quite well. People getting into programmers shouldn’t choose D over the more favoured legacy languages like C/Java/Basic.

Getting started with D

There are three popular compilers for the D programming language. GDC is based on top of the GNU Compiler Collection, LDC is based on LLVM and DMD is the reference compiler created bu the same person that made D, Walter Bright. GDC and LDC are great compilers since they produce more performant binaries than DMD does but they are difficult to set up in Windows than DMD. We’re focusing on the Windows OS in this article.

Go to the DMD download page (http://dgit.in/DMDDown) and get the installer for Windows. Once you’ve downloaded the zip file, simply unzip it in any location and add the path for the binaries in the environment variables for Windows. It should be “dmd2\windows\ bin”. This is only if you are using just one version of DMD, if there are more than one installations of DMD then there will be a conflict during calls and you’ll run into a few errors or warning over version differences. You can always use the installer and get it done with since it too does exactly the same thing. Once you have the path configured, open up command prompt and type ‘dmd’ and press enter. Invoking DMD in this manner without any extra arguments will cause it to display the version number along with all the valid command line options. If you do see those then your installation went fine and you can begin programming in D. You can also use IDE bundles like DDT, Mono-D, Visual-D and Code::Blocks. You can now start programming in D. All source files are to be saved with a .d extension. As is customary with any new programming tool, here is a hello world program for D.

INLINE BOX CODE:
import std.stdio;
void main() {
writeln(“Hello World!”);
}
Once you have saved the program (let’s call it hello.d), simply navigate to the folder in a command prompt window and type “dmd hello.d”. Your console should have a new line as Hello World!, if this is not the case then there was a problem in locating the compiler binary, recheck the installation and ensure that the binaries have been added to the path.

Speed

D is as fast as C++ if you use the right compiler and also the correct arguments when compiling on a platform. Using the default arguments can lead to code that is ridiculously slow. Here is a little benchmark that was run on a machine with code written in C and D to perform the exact same operations. The code was then compiled using different compilers and using different arguments on each compiler to figure out which was the best way to go about using D versus C++.

The results do shed light on D’s claims on performance. The results are mentioned below and you can visit http://dgit.in/DversusC for the complete discussion on the same.
D (const ref arguments, dmd compiler, correct optimization): 9.6s
D (in arguments, dmd compiler, correct optimization): 6.1s
D (in arguments, gdmd compiler, correct optimization): 3.9s
C++: 3.8s

We see that the default compiler DMD isn’t that great, especially when it adds 2.2 seconds to the overall runtime despite having the correct optimization and the right arguments being passed to the compiler. However, when an alternative compiler (gdmd) was used with the proper arguments and all optimization done, D performed just as good as C++ does. So would you say it’s worth the effort?

Resources and further reading:

• http://dlang.org/download.html
• http://dlang.org/spec.html
• http://www.tutorialspoint.com/d_programming/index.htm
• http://www.dprogramming.com/tutorial.php
• http://ddili.org/ders/d.en/index.html
• http://www.prowiki.org/wiki4d/wiki.cgi?FrontPage
• http://www.wikiservice.at/d/wiki.cgi?EditorSupport
• http://dui.sourceforge.net/
• http://www.dsource.org/projects/build/
• http://www.dprogramming.com/dfl.php

Go to Page: 1 2

Google
Ashutosh Gupta
Share a little biographical information to fill out your profile. This may be shown publicly. In WordPress version 2.9, a new filter was added to make changing the [...] string in the excerpt. Do not use both of these methods in