Intro
This is a continuation of the 12in24 challenge, in which I/you/we learn 12 new coding languages (or at least the basics) in one year.
I spent about a week learning D, and here is some of what I learned.
D
Installation
You can install dmd
(the D compiler) from the official downloads page.
DMD stands for Digital Mars D, although I'm not quite sure why. Maybe the creator liked Mars or Mars bars.
Getting started
Writing your first D program
We all know what's coming (or maybe not...)
module hello;
import std.stdio;
// string[] args is optional
void main(string[] args) {
writeln("Hello, World!");
}
Compile it with this:
$ dmd hello.d
$ ./hello
#=> Hello, World!
Now another simple program, FizzBuzz:
module fizzbuzz; // declare a module name, used when importing packages (we'll see that soon)
import std.stdio : writeln, writefln; // import the writeln and writefln functions from std.stdio, but nothing else.
void main() {
int start = 0;
int end = 100;
// one way of doing a for loop
for (int i = start; i < end; i++) {
if (i % 15 == 0) {
writeln("FizzBuzz");
} else if (i % 3 == 0) {
writeln("Fizz");
} else if (i % 5 == 0) {
writeln("Buzz");
} else {
writefln("%d", i); // replaces %d with i
}
}
// this is the more common way to do a for loop:
foreach(i; start..end) {
// same as above
}
}
The syntax, you might realize, is extremely similar to that of C. And it is. D is supposed to be another iteration of the C language.
You can see the stuff I did here.
Using packages
To create a D project, we use dub
, which is D's package manager. It should have come preinstalled with D.
To create a project, run this command:
$ dub init
Fill out the prompts, and you have a full-on project!
Edit source/app.d
to do whatever you want, like print "Blah blah blah" 500 times, or do a guess the number game.
You can then run your project with this command:
$ dub run # run the project
$ dub build # build an executable for the project into ./<project_name>
You can follow the READMEs in the repo or follow the D documentation.
Conclusion
D is a very cool seeming language. It's very feature rich, beginner friendly (no pointers!), and is simple to learn. Some cons might be a (relative to other languages) smallish standard library, and a smaller community.
Feel free to ask questions :)