D, the Best Programming Language, for Former Python Developers
D, the Best Programming Language, for Former Python Developers
Victor Porton, 2020
Abstract
D programming language or DLang for Python programmers: rapid software development of high performance software and a reliable software technology. There is no best programming language, they say. There is, for most applications it is D.
The book starts from a comparison of D to Python. Then it explains D starting from simple features (types, variables, functions, expressions, statements) up to advanced ones (object oriented programming, templates, mixins, contract programming, overloading of operators, etc.) Then follows an advice for good programs and introduction to several libraries: the standard library Phobos (ranges, arrays, functional programming, tuples, the variant types, files, etc.) and several other important libraries including some libraries written by the author. Through these libraries D supports asynchronous programming, multithreaded programming, dependency injection (one particular ideologically correct flavor of which is introduced in details), etc. This book doesnt just teach you D syntax, it gives you some principles to program in D in the right way. It is also considered how to create an object oriented wrapper over a C library. The book contains the articles How to Make Your Programs Run Hundreds to Thousands Times Faster Without Giving up Reliability and Ease of Programming and What would be my choice of programming language if I was ordered to write a nuclear weapon control system? D is the second choice for such kinds of projects after Rust (and no, it is not Ada). The book ends with an advice on which D compiler to use.
This book makes you a D programmer. D is a very unusually good programming language for startups, academics, programming hobbyists, open source developers, big and small companies who decided to improve their efficiency and effectivity, electronics companies, game developers, system programmers, military, industry, supercomputers and mainframes. D can be used for desktop, Web development, electronics, smartphones, servers, mathematics, robots, games, GPU and AI, and even for writing an OS. If you are a programming teacher, your service would be greatly improved by starting to teach D. If you belong to any of these categories (and probably many more), dont miss this great opportunity to learn D! And D is fast and low electricity consuming (green).
About the author
Victor Porton is a free software (especially XML and blockchain) and mathematics developer (author of algebraic general topology and axiomatic theory of formulas), a religious (Christian) writer and preacher. Victor studied mathematics at Perm State University and did his first (big) scientific discovery at the first year of the study.
Victor Porton is the developer of several open source D libraries, especially some foundational template libraries.
Introduction
Python nowdays is an established, mainstream language.
This course teaches programmers who already know Python to switch to a new yet little known language called D. It is a step forward: D is one of the best (Id say the best) of all programming languages and has certain advantages over Python.
After completion of this course, you will be able to write programs in D, fluently and idiomatically. After this, some of you (most of those whose employees dont insist on using Python) could forgot Python as not needed anymore old tool.
D is also called dlang or DLang or Dlang. (When you search about D on Google, type "dlang"
not "D"
.)
Notable organizations that use the D programming language for projects include Facebook, eBay, and Netflix. 1
D is defined by Language Reference. 2
Historic note: Originally (before a wide release) D language was called by the author Mars language but later renamed to D under influence of his friends calling it so. I think this was a marketing error.
In this book some small fragments of the D Language Reference 2 are used without a specific attribution.
A comparison of Python and D
Python is a dynamic programming language (or what is the same a language with dynamic types or language with weak typing system, see a detailed explanation below). This means that in Python every variable is declared without a type (except of non-enforced annotations of function arguments and return values) and every variable can change its type at any moment.
Python was conceived as a dynamic language because dynamic languages are considered more convenient and powerful than static languages (or what is the same languages with strict types).
The power of flexibility that Python gives to programmers was exciting at its time and continues to excite many programmers nowdays. AFAIK, Python was the first language giving programmers so much power of flexibility.
Most legacy languages (such as C or Pascal) were statically typed. Dynamic languages became popular with advent of more powerful computers, because (see below) dynamic languages require much more computing power (or time) to do the same task than a static language.
So, many modern programmers not without a reason choose dynamic languages for everyday work.
It looked like that the main trend of the evolution of programming languages was shift from static languages to dynamic languages. It was so until advent of D, because D is a static language offering almost as much power of flexibility as dynamic languages such as Python. If people have any humanity then in the future static languages will take over, because now we have D language and possible its future successors what are static but nevertheless very flexible.
Another reason to switch to a static language like D is that D offers much greater reliability (that is less bugs in your software) than Python.
Advantages and disadvantages of D
Advantages
D programming language is the newest and the only technology that is both:
high performance
rapid development
reliable
High performance means that your software is expected to load at least about 10 times faster (for SSD disks) and run hundreds to thousands of times faster than most modern software and also use much less memory and produce less heat at the CPU (save electricity).
Rapid development means that you do the project quickly. D programming is as easy for experienced programmers as Python or Ruby programming, if not easier thanks to higher reliability (see below) and thus less time to debug.
Reliable means that you use no less (maybe more) reliable technologies than in the atomic powerplant that powers your house and nuclear rockets and airplanes, not what other developers do.
D is also open source just like Python.
D has true multicore CPU support, unlike Python that always uses only one CPU core, even if the program is multithreaded, Python just switches execution of several threads in just one CPU core.
Finally, D compiles for a real CPU not to a bytecode as Python. It allows much faster execution of programs. It also has the advantage that you do not need to distribute a D compiler together with your software (unlike Python, programs written in which require a Python interpreter to run).
Disadvantages
To be honest, D has a few deficiencies:
The program pauses at arbitrary moments of time to do memory cleanup.
Writing mathematical software in D may sometimes be more difficult than in Python, because there is no suitable (execution time) notion of arbitrary type in D and math software sometimes wants functions that accept some kind of a tree of arbitrary objects. However, see