📜 ⬆️ ⬇️

D programming language

Good all the time of day!
There was a desire (and even an opportunity!) To tell the community about the wonderful programming language D. Sadly, there is still a minimum of information on the network, and even less information on a habr.
Everything is likely to even know about him, but very few people tried to write projects on him, and all because he, you see, “did not shoot,” that he had no future. The main idea with which I am writing this article is to dispel some misconceptions about this undoubtedly remarkable programming language, and at the same time, of course, just give to the broad masses some more information about it.

What are we talking about?

Yes, first of all, I don’t want to write “a 200-line code mail server,” as haskella fans do. This undoubtedly demonstrates the power of 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) for C ++ - as a language in which if opportunities 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 either a class class natural for static languages ​​or a prototype accepted in dynamically typed languages.

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

First of all, I would like to show that the D language supports all these styles, 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. *

First, some general information:

D is a compiled, statically and strongly typed language with a c-like syntax. Positioned as a modern language for system programming. In the language there is garbage collection, which, in principle, does not interfere with quietly doing it by hand, working directly with a bunch of C - through the well-known malloc and free, although this is not recommended.

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

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

Well, here you can hardly think of something original, so the function is not very meaningful, but still:

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


A nice addition compared to C ++ is the nested functions (in this place the programmers on pascal triumph):

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


Very handy for encapsulation, isn't it?
This is probably all that can be said about the procedural style, let's move on to the more interesting.

Functional style: firstly, it is characterized by the fact that functions are objects of the first kind:

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


For the curious, here are the ads without automatic output type:

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


Moreover, in D you can create delegates - functions that support closures.

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


Functions and delegates are easy to manipulate, they are very transparent and the code is not noisy due to type inference.
In principle, according to my feelings, D is not less functional than Python, although it will not reach haskell.

In addition, I will mention another functional "chip": pure functions. We'll see:

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


This means that the function is pure, that is, it does not contain and does not change the state, both of its own and of the system as a whole and for the same arguments will give the same values.

Such functions can be performed, for example, in parallel, their result can be cached, that is, the compiler receives a huge field for optimization.

I note that he also monitors the correctness. You can't do it like this:

 int c = 1; pure int inc(int a) { return a+c; // ,      . } 
int c = 1; pure int inc(int a) { return a+c; // , . }


We now turn to object oriented style.
First of all, it will not surprise anyone to create classes and instantiate objects of these classes:

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


Even the ability to inherit them will surprise no one!

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


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 will add that in language there is no multiple inheritance of classes, only multiple inheritance of interfaces.
There is also a trick in the language: the multiple inheritance of classes can be simulated without disturbing the tree structure of the graph of kinship of classes. For those who are interested, I will not reveal the intrigue - read the book “The D programming language” by Alexandrescu. ****

This is a very fun and, at the same time, handy feature.

What else did I promise? Oh yes, prototype inheritance.

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

However, this is also possible here (forgive me for a lot of lines, but you yourself know that the OOP code is a bit bold):

 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(); } 
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(); }


Moreover, for the lazy and lovers of beauty there are anonymous classes, rewrite a piece of code:

 A createA(string s) { return new class A //  ,    A { this() { s = ""; } this(string s) {this.s = s;} override string toString() { return "trollface"; } }; } 
A createA(string s) { return new class A // , A { this() { s = ""; } this(string s) {this.s = s;} override string toString() { return "trollface"; } }; }


So, all the announced concepts of programming are supported by the D language, and, moreover, quite concisely.
Of course, this is not enough for the language to become good. To make it truly powerful and convenient, you need a beautiful and elegant interaction of all these concepts.

It would be nice, for example, if a javascript programmer and a C ++ programmer could write one project on D together, write like they were used to in their field, or at least approach it, and at the same time so that neither readability is lost neither efficiency, and more importantly, that both styles can organically and without crutches interact with each other.

But this is a topic for another article.

I myself am just learning this wonderful language, so:

Firstly, if there are gurus here who find mistakes, I will be happy to hear instructions.

Secondly, I am not going to stop there, and if the community becomes interested, I will be happy to continue writing articles on D as it is being studied.

Thirdly, if you have any questions, you are 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 “has not taken off”, that people are numb, or they are afraid of a new one, and in any case, the language is worth it to switch to it from many others, or, would consider it as a worthy alternative.

* for many.
** yes, caught - again for many.
*** And at the same time the code will not become a frank govnokod - how it becomes when moving, for example, from C ++ to haskell (this is me about myself :), but it fits into the general style of the language.
**** And you really thought that the name of the article I came up with?

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

Source: https://habr.com/ru/post/135044/


All Articles