D ( Di ) is a multiparadigm compiled programming language created by Walter Bright of Digital Mars . Since 2006, the co-author is also Andrei Alexandrescu . Initially, D was conceived as a reengineering of the C ++ language , but, despite the significant influence of C ++, is not its option. Also, the language has experienced the influence of concepts from the programming languages Python , Ruby , C # , Java , Eiffel .
| D | |
|---|---|
| Semantics | multi-paradigm : imperative , object-oriented , functional , contractual [1] , generalized programming |
| Language class | , , , , , , and |
| Execution type | compiled |
| Appeared in | 2001 |
| Author | Walter Bright , Andrei Alexandrescu |
| Developer | and the |
| File extension | , , or |
| Release | 2.087 ( 2019-07-01 [2] ) |
| Type system | strict, static, with type inference |
| Basic implementations: | Digital Mars D (reference implementation) , LDC , GDC |
| Experienced influence | C , C ++ , Python , Ruby , C # , Java , Eiffel |
| Influenced by | MiniD , DScript , Vala , Qore , Swift , Genie |
| Site | dlang.org |
When creating the D language, an attempt was made to combine the performance of compiled programming languages with the security and expressiveness of dynamic languages .
The stable version of the compiler 1.0 was released on January 2, 2007 [3] .
The next version of the compiler 2.0 (the latest major version for today) began development on June 17, 2007 [4] and is still being developed.
D is available for Windows, Linux, Mac OS, FreeBSD operating systems. Work is underway on porting to Android [5] .
Content
Language Overview
Syntax
D refers to a family of C-like languages with curly brackets, in general terms its syntax is similar to C / C ++ / C #, Java. When developing a language, the principle is observed: a code that is equally valid in C and D must behave the same way.
“ Hello, world! "On D:
import std . stdio ;
void main ()
{
writeln ( "Hello, world!" );
}
Just like in C, the main() function is the entry point.
Universal Function Calling Syntax (UFCS)
In D, a UFCS (Uniform function call syntax) mechanism is implemented that allows calling functions for any object as if they are its methods. For example:
import std . stdio ;
import std . algorithm ;
import std . array ;
void main ()
{
auto a = [ 2 , 4 , 1 , 3 ];
// all three following options are correct and work the same
writeln ( a ); // "classic" C-like variant
a . writeln (); // the function is called as if it were a method of the object "a", although it is not such
a . writeln ; // function without parameters can be called without parentheses
// this allows you to use call chains that are characteristic of functional languages
int [] e = a . sort (). reverse ;
// multiline call chain also possible
stdin
. byLine ( KeepTerminator . yes )
. map ! ( a => a . idup )
. array
. sort ;
}
Function Attributes
Functions in D can be defined with additional optional attributes that allow you to explicitly specify some aspects of the behavior of these functions. For example, a function marked with the pure attribute is guaranteed to be functionally clean (with some reservations) [6] . Functional cleanliness is checked at the compilation stage. An example of a function declaration with an attribute:
pure int sum ( int first , int second )
{
return first + second ;
}
int sum ( int first , int second ) pure // attributes can also be specified after the argument list
{
return first + second ;
}
Examples of function attributes:
- pure - functional cleanliness
- @safe - a guarantee of safe work with memory
- nothrow - the function is guaranteed not to generate exceptions
- @nogc - a guarantee that the function does not contain operations that require garbage collection
- @property is an attribute of the class method that allows avoiding the use of “naive” setters-getters
Built-in unit tests
In D, unit tests are part of the language, you can use them without connecting additional libraries or frameworks.
import std . stdio ;
int first ( int [] arr ) {
return arr [ 0 ];
}
unittest {
int [] arr1 = [ 1 , 2 , 3 ];
int [] arr2 = [ 10 , 15 , 20 ];
assert ( first ( arr1 ) == 1 );
assert ( first ( arr2 ) == 10 );
}
void main () {
// ...
}
There are many functions implemented in the language, which were absent at that time in C ++: contract programming , embedded unit tests , modules instead of header files, garbage collector , built-in associative arrays, closures , anonymous functions , the template mechanism was significantly reworked.
Programming Paradigms
D implements five basic programming paradigms - imperative , OOP , metaprogramming , functional programming, and parallel computing ( actor model ).
Memory Management
D uses the garbage collector to manage memory, but it is also possible to manually control it by overloading the new and delete operators, as well as using malloc and free , similarly to C. The garbage collector can be turned on and off manually, you can add and remove memory areas from its visibility, force a partial or complete build process. There is a detailed guide describing the various memory management schemes in D for those cases where the standard garbage collector is not applicable.
SafeD
SafeD is the name of a subset of the D language, the use of which guarantees the security of memory access .
Data Types
The language has a rich set of specific data types and tools for defining new types. Types in the D language are divided into value types and reference types.
Base Types
The set of basic types can be divided into the following categories [7] :
void- special type for empty valuesbool- a logical type- integer types: signed
byte,short,int,longand their corresponding unsignedubyte,ushort,uint,ulong - types for floating-point numbers:
float,double,real. For floating-point types, there are corresponding options for imaginary and complex numbers:- imaginary:
ifloat,idouble,ireal - complex: with
сfloat, withсdouble, withсreal
- imaginary:
- character (character) types:
char,wchar,dchar, denoting the code units of the UTF-8, UTF-16 and UTF-32 encodings, respectively.
Unlike C ++, all sizes of integer types are defined by specification. That is, the int type will always be 32 bits. Integer literals can be written in decimal, binary (with prefix 0b) and hexadecimal (with prefix 0x) number system. The way of writing literals in the octal system in the C style (that is, with the prefix 0) was removed, since such a record is easily confused with decimal. If you still need to use the octal system, you can use the template std.conv.octal .
Derived Types
pointerarrayis an arrayassociative array- associative arrayfunction- functiondelegate- delegatestring,wstring,dstring- convenient aliases for immutable arrays of signed (character) typesimmutable(char)[],immutable(wchar)[]andimmutable(dchar)[], denoting immutable ( immutable qualifier) Unicode strings in one of the UTF encodings -8, UTF-16 and UTF-32, respectively.
Custom types
aliasis a pseudonymenum- listingstruct- structureunionclass- class
Type inference, keywords “auto”, “typeof” and unnamed (“Voldemort”) types
D implements a type inference mechanism. This means that a type, as a rule, can be computed at compile time and it is not necessary to specify it explicitly. For example, the expression: auto myVar = 10 will be converted to int myVar = 10 at the compilation stage. Using type inference has several advantages:
- More concise and readable code, especially if it uses long names of structures or classes. For example, the expression
VeryLongTypeName var = VeryLongTypeName(/* ... */);
can be replaced by
auto var = VeryLongTypeName(/* ... */);
- Using the typeof keyword, you can create a variable of the same type as an existing variable, even if its type is unknown. Example:
// file1.d
int var1 ;
// file2.d
typeof ( var1 ) var2 ; // var2 gets type int
- use of nameless types. Example:
// The function actually returns the result of the TheUnnameable type, but since this type is defined inside the function,
// we cannot explicitly set it as the return type.
// However, we can set the return type as "auto", letting the compiler calculate it itself
auto createVoldemortType ( int value )
{
struct TheUnnameable
{
int getValue () { return value ; }
}
return TheUnnameable ();
}
Unnamed types are informally called Voldemort-types by analogy with Volan de Mort (“He-Who-One-Can't Call”), the main antagonist of the Harry Potter series [8] . Type inference should not be confused with dynamic typing , since although the type is not explicitly specified, it is calculated at the compilation stage and not at run time.
Implementations
- DMD - Digital Mars D ( VIKI ), a reference compiler developed by Walter Bright. This compiler most fully implements the standard language, support for all innovations appears in it in the first place. Front-end and back-end ( [1] ) are distributed under the Boost license.
- LDC - DMD ( VIKI ) - Front End for LLVM ( SITE )
- GDC - DMD ( VIKI ) - Front End for GCC Compiler
- LDC for IOS - LDC-based toolkit for cross-compiling to iOS
- D for Android - Toolkit for cross-compiling to Android (x86 using DMD and ARM using LDC) instruction
- SDC (Dumb Compiler D) is an experimental compiler (compiler as library) using LLVM as a back end and not based on DMD.
- Calypso - LDC fork for direct headers directly.
- MicroD - DMD
- DtoJS - DMD fork
- D Compiler for .NET - A back-end for the D programming language 2.0 compiler [9] [10] . It compiles the code to Common Intermediate Language (CIL) bytecode rather than to machine code. The CIL can then be run through a Common Language Infrastructure (CLR) virtual machine .
- DIL is a hand-crafted compiler using the Tango standard library. The lexer and the parser are fully implemented. Semantic analysis is being worked on. The backend will most probably be LLVM .
Development Tools and Tools
IDE and Editors
Support for D in various IDEs implemented using plugins:
| IDE | Plugin | Platforms |
|---|---|---|
| IntelliJ IDEA | DLanguage | cross platform |
| Eclipse | DDT | cross platform |
| MonoDevelop / Xamarin | Mono-d | cross platform |
| Visual studio | Visual-d | Windows |
| Visual studio code | Code-d | cross platform |
| Xcode | D for Xcode | Mac os x |
| Zeus IDE | D for Zeus IDE | Windows |
Native IDE for D language:
- Coedit (Windows, Linux)
- DLangIDE (Windows, Linux, OS X) - the environment is created on D itself, supports syntax highlighting, code completion, DUB, various compilers, debugging, and much more.
D is supported in a variety of text editors: Vim, Emacs, Kate, Notepad ++, Sublime Text, TextMate, and others [11] .
Package Manager
DUB is the official package manager D. DUB serves as a package repository and is used to manage dependencies, as well as an assembly system. A set of dependencies, project metadata and compiler flags are stored in JSON or SDL format. An example of a simple project file (JSON):
{
"name" : "myproject" ,
"description" : "A little web service of mine." ,
"authors" : [ "Peter Parker" ],
"homepage" : "http://myproject.example.com" ,
"license" : "GPL-2.0" ,
"dependencies" : {
"vibe-d" : "~> 0.7.23"
}
}
Utilities and Tools
rdmd
rdmd is a utility bundled with the DMD compiler that allows you to compile and run D source files on the fly. This allows you to use D for small programs like bash and perl:
// myprog.d
# ! / usr / bin / env rdmd
import std . stdio ;
void main ()
{
writeln ( "Hello, world with automated script running!" );
}
Calling the command ./myprog.d in the console will automatically compile and run the program.
DPaste
Dpaste is an online service for launching D programs in a browser, similar to the JSBin and CodePen services .
asm.dlang.org
asm.dlang.org is an online compiler and disassembler.
Use, distribution
The distribution of the D language is limited, but it is used for real industrial software development. The site [2] provides a list of 26 companies that successfully use D in the development of software systems operating in various fields, including system programming, web projects, games and game engines, software for scientific calculations, utilities for various purposes and so on. The D Language Foundation is promoted, in particular, by the D Language Foundation, a non-governmental organization that promotes the D language itself and open source software created with its use.
According to the TIOBE index , the maximum interest in D was manifested in 2007–2009, and in March 2009, the D language index reached 1.8 (12th place), which is its absolute maximum. After a decline in the first half of the 2010s, by 2016 it came in a relatively stable state - the numerical value of the index fluctuates in the range of 1.0-1.4, in the ranking the language is in the third ten. In the popularity rating compiled based on the results of aggregation of data on developer vacancies, the D language is not included in either the main (top-20) or the general (top-43) list, which indicates a low demand among employers.
Code examples
Example 1
“ Hello, world! "
import std . stdio ;
void main () {
writeln ( "Hello, world!" );
}
Example 2
The program that outputs the command line arguments with which it was called
import std . stdio : writefln ;
void main ( string [] args )
{
foreach ( i , arg ; args )
writefln ( "args [% d] = '% s'" , i , arg );
}
Example 3
A program that reads a list of words from a file line by line and displays all the words that are anagrams of other words.
import std . stdio , std . algorithm , std . range , std . string ;
void main ()
{
dstring [] [ dstring ] signs2words ;
foreach ( dchar [] w ; lines ( File ( "words.txt" )))
{
w = w . chomp (). toLower ();
immutable key = w . dup . sort (). release (). idup ;
signs2words [ key ] ~ = w . idup ;
}
foreach ( words ; signs2words )
{
if ( words . length > 1 )
{
writefln ( words . join ( "" ));
}
}
}
See also
- Digital mars
- For a comparison of D features with other languages, see Comparison of programming languages.
Notes
- ↑ Description of the implementation of the contract programming paradigm in the Di language . Archived January 3, 2014.
- ↑ List of changes in the compiler of the language Di .
- ↑ DMD 1.00 - here it is! (digitalmars.D.announce) Archive dated March 5, 2007 on Wayback Machine (eng.)
- ↑ D 2.0 changelog . The date of circulation is January 11, 2009. Archived June 1, 2012. (eng.)
- ↑ D for Android - D Programming Language Discussion Forum
- ↑ Functions - D Programming Language
- ↑ Types - D Programming Language
- ↑ Voldemort Types In D | Dr dobb's
- ↑ CodePlex Archive . CodePlex Archive. The appeal date is February 16, 2018.
- ↑ Source for the D.NET Compiler is Now Available . InfoQ. The appeal date is February 16, 2018.
- ↑ Editors - D Wiki
Literature
- Alexandrescu A. Programming Language D = The D Programming Language. - Symbol plus , 2012. - 544 p. - ISBN 978-5-93286-205-6 .
- Official Wiki
- Programming Language D - Book
- Programming in D - Book
- Interesting articles dedicated to D
- Textbook D in Russian
- Information on language D in Russian
Links
- D Programming Language (English) . - official site. The appeal date is May 10, 2012. Archived May 18, 2012.
- D on GitHub .
- code.dlang.org (English) . - package repository for D and the site of the official package manager DUB.
- Website of the D language in Russia (March 14, 2013). Archived March 15, 2013.
- Open Source Development for the D Programming Language (English) . - free software on / for language D. Date of application January 8, 2006 . Archived May 18, 2012.
- GDC - D Programming Language for GCC (Eng.) (May 27, 2007). - D programming language for GCC . The appeal date is July 17, 2011.
- LDC - D Programming Language for LLVM (English) . - D programming language for LLVM . Archived May 18, 2012.
- Alexandrescu, Andrew The Case for D (Eng.) (June 15, 2009). The appeal date is January 7, 2011. Archived May 18, 2012.
- Translations of textbooks and the standard library of the D language
- Examples of D code in the Rosetta Code project
,