D programming language

Published on December 21, 2011

D programming language

Good day to all!
There was a desire (and even an opportunity!) To tell the community about the wonderful programming language D. Unfortunately, the network on it still has a minimum of information, and even less on the hub.
Everyone is likely to even know about him, but few tried to write projects on him, and all because he, you see, “didn’t shoot”, that he has no future. The main idea with which I am writing this article is to dispel some misconceptions about this undoubtedly wonderful programming language, and at the same time, of course, just give a little more information about it to the masses.

What will it be about?

Yes, the first thing I do not want to write is a “mail server with 200 lines of code,” as Haskell fans do. This undoubtedly demonstrates the power of the language, but this is not the purpose of this article.

And I will follow the idea of ​​the authors of the language, and indirectly promote it as a replacement (or analogue) of C ++ - as a language in which if there is opportunity for everything, and as a worthy competitor to other modern languages.

We all know the basic programming styles: procedural, object-oriented, functional. Moreover, object-oriented can be both a class natural for static languages ​​and a prototype adopted in dynamically typed languages.

For know-it-alls: do not swear, of course, I have listed not all, but the main directions of modern applied programming.

First of all, I would like to show that the D language supports all these styles, and in a completely natural way (at the risk of running into disapproval: it is much more natural than C ++), that this language is literally for everyone. *

To begin with, a little general information:

D - compiled, statically and strongly typed language with si-like syntax. It is positioned as a modern language for system programming. There is garbage collection in the language, which, in principle, does not prevent you from quietly doing it manually, working directly with a bunch of si - through the well-known malloc and free, although this is not recommended.

There is nothing radically new (well, almost, but we will not go into it) in the language, its main advantage is convenience for everyone. ** What does this mean? This means that the language combines the power and expressiveness (and, accordingly, it also reduces the development time) of languages ​​such as Java and C #, but does not lose direct communication with the system at the C level (and the speed of the compiled code!).
This means that almost every programmer who is used to writing applications in% langname% will be able to easily and easily adapt to D without even changing his programming style! ***
Well, this is all advertising, let's move on to the specifics.

So, I promised to show that you can write in a language (within reasonable limits) using any style.
Well, let's start with a simple one: procedural.

Well, here it’s hardly possible to come up with something original, so this is not a very meaningful, but still a function: Nested functions are a nice addition to C ++ (programmers triumph in Pascal at this point): Very convenient for encapsulation, not so is it? This is probably all that can be said about the procedural style, we will move on to a more interesting one. Functional style: firstly, it is characterized by the fact that functions are objects of the first kind: For the curious, here are announcements without automatic type inference: Moreover, in D, you can create delegates - functions that support closures. Functions and delegates are easy to manipulate, they are very transparent and the code is not noisy due to type inference.

int inc(int a)
{
	return a+1;
}





int incTwice(int a)
{
	int inc(int a)
	{
		return a+1;
	}
	return inc(inc(a));
}








int main()
{
	auto inc = (int a) {return a+1;};
	writeln(inc(1));
}





int function(int) inc = function (int a) {return a+1;};





int c = 1;
int delegate(int) inc = delegate (int a) {return a+c;};




In principle, in my opinion, D is no less functional than Python, although it does not reach haskell.

In addition, I’ll mention one more functional “trick”: pure functions. Let's see: This means that the function is pure, that is, it does not contain and does not change the state of both its own and the system as a whole and will give the same values ​​for the same arguments. Such functions can be performed, for example, in parallel, their result can be cached, that is, the compiler gets a huge field for optimization. I note that he also monitors the correctness. You can’t do it like this: Now let's move on to the object-oriented style. Firstly, it will not be a surprise to anyone to create classes and instantiate objects of these classes:

pure int inc(int a)
{
	return a+1;
}









int c = 1;
pure int inc(int a)
{
	return a+c;	// ошибка, программист не прочитал определение чистой функции.
}






class A
{
	int v;
	int inc(int a)
	{
		return a+1;
	}
}



Even the ability to inherit them will not surprise anyone! And if it doesn’t surprise, it means that the initial message can be considered confirmed - it will be easy for programmers in OOP languages ​​to adapt. I add that in the language there is no multiple inheritance of classes, only multiple inheritance of interfaces. There is also a trick in the language: multiple inheritance of classes can be simulated without violating the tree structure of the graph of related relations between classes. For those who are interested, I will not reveal the intrigue - read Alexandrescu's book “The D programming language.” **** This is a very funny and, at the same time, convenient feature. What else did I promise? Oh yes, prototype inheritance.

class B : A
{
	int v;
	override int inc(int a)
	{
		return a+2;		// trollface
	}
}












Everyone who writes in languages ​​like C ++ is not used to seeing such things and writing in their code. It would seem that this is unnatural for compiled languages ​​due to static typing.

However, this is also possible here (sorry for a lot of lines, but you yourself know that the OOP code turns out to be a little bold): Moreover, for lazy and lovers of beauty there are anonymous classes, we rewrite a piece of code: So, all announced programming concepts are supported by the language D, and at the same time is quite concise. Of course, this is not enough to make the language good. For it to become truly powerful and convenient, you need a beautiful and elegant interaction of all these concepts.

class A
{
	string s;
	this() { s = ""; }		// да, так называются конструкторы, по-моему удобней, чем по названию класса.
	this(string s)
	{
		this.s = s;
	}
	override string toString()	// перегрузим метод корневого класса Object
	{
		return s;
	}
}
A createA(string s)
{
	class B : A		// точно, вложенные в функцию классы!
	{
		this() { s = ""; }	
		this(string s)	// конструкторы не наследуются.
		{
			this.s = s;
		}
		override string toString() { return "trollface"; }
	}
	return new B(s);
}
void main()
{
	A a = new A("hi!");
	A b = createA("hi!");
	writeln(a, " : ", b);		// ну да, выведет "hi! : trollface"
	readln();
}





A createA(string s)
{
	return new class A		// это значит, что наследуется от A
	{
		this() { s = ""; }
		this(string s) {this.s = s;}
		override string toString() { return "trollface"; }
	};
}






It would be nice, for example, for a javascript programmer and a C ++ programmer to write one project on D together, to write as they used to in their field, or, at least, to approach this, and at the same time so as not to lose any readability nor efficiency, and more importantly, that both styles can interact seamlessly and without crutches with each other.

But this is a topic for another article.

I myself am only learning this wonderful language, so:

Firstly, if there are gurus here who find errors, I will gladly listen to the instructions.

Secondly, I’m not going to stop there, and if the community is interested, I will be happy to continue to write articles about D as I study it.

Thirdly, if questions arise, I’m happy to answer, discuss and debate, only without fanaticism.

And in conclusion, I hope that my article will help you realize that this is not a language that hasn’t “taken off”, that people are numb, or are afraid of a new one, and in any case, the language is worth it to switch to from many others, or, although to consider it as a worthy alternative.

* for many.
** Yes, yes, caught - again for many.
*** And the code at the same time will not become a frank shit - what it becomes during the transition, for example, from C ++ to haskell (this is about me :), but it fits into the general style of the language.
**** And did you really think that I myself came up with the title of the article?

PS
As I was hinted at in the comments, Python still has closures. Apparently, I tried it for a long time, or something has changed, or I'm just forgetful. I apologize.