D (programming language)
This page was last modified on 8 June 2016, at 21:00.
Paradigm | compiled, multi-paradigm: procedural, object-oriented, functional, generic |
---|---|
Designed by | Walter Bright, Andrei Alexandrescu (since 2006) |
Developer | Digital Mars, Andrei Alexandrescu (since 2006) |
First appeared | 2001 |
Typing discipline | strong, static, inferred |
License | [[Boost Software License|Boost]] (DMD frontend,[1] standard and runtime libraries),source available (DMD backend),Fully open-source (LDC and GDC)[2] |
Website |
dlang |
Major implementations | |
DMD (reference implementation), GDC, LDC | |
Influenced | |
MiniD, DScript, Vala, Qore, Swift,[3] Genie | |
D — multiparadigmality compiled programming language created by Walter Bright of the company Digital Mars. Since 2006, the co-author is also a Andrei Alexandrescu. Initially D was designed as a re-engineering of language C ++, however, in spite of the significant influence of C ++, is not his goal. Also, the language was influenced by the concepts of programming languages Python, Ruby, C #, Java, Eiffel.
When you create a language in an attempt was made to combine the performance of compiled programming language security and expressiveness dynamic. The stable version 1.0 of the compiler came January 2, 2007. The stable version 2.0 compiler (the latest for today major version) released on June 17, 2007. D is available for operating systems Windows, Linux, Mac OS, FreeBSD. Work on porting to Android.
Contents
Language overview
Syntax
D belongs to the family C-like languages with braces, in general terms its syntax is similar to C / C ++ / C #, Java. In the development of the language, the principle: the code, the same is valid in C, and D, should behave the same way. "Hello, world!" in D:
import std.stdio;
void main() {
writeln ("Hello, world!");
}
As in C, the function main () </ code> is the entry point.
Universal function call syntax (UFCS)
In a mechanism in UFCS (Uniform function call syntax), allows you to call functions for any object as if they are his methods. For example:
import std.stdio;
import std.algorithm;
import std.array;
void main()
{
auto a = [2, 4, 1, 3]; writeln(a);
a.writeln();
a.writeln;
int[] e = a.sort().reverse;
stdin
.byLine(KeepTerminator.yes)
.map!(a => a.idup)
.array
.sort;
}
Attributes of functions
D functions may be defined with additional optional attributes that allow to explicitly specify some aspects of the behavior of these functions. For example, the function that are marked 'pure' is guaranteed to be functional clean (with some reservations). Functional purity while checked at compile time. Example of declaring a function with the attribute:
pure int sum (int first, int second)
{
return first + second;
}
int sum (int first, int second) pure
{
return first + second;
}
Examples of attributes of functions:
- Pure - functional purity
- safe - Guarantee safe operation of the memory
- Nothrow - function is not guaranteed to generate exceptions
- nogc - A guarantee that the function does not contain any operations that require garbage collection
- property - Attribute class method avoids the use of "naive" getters-setters
Built-in unit tests
The language D unit tests are part of the language, they can be used without 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() {
// ...
}
The language provides many features not available in C ++: contract programming built unit tests, Modules instead of header files, GC, built-in associative arrays, circuit, anonymous functions significantly redesigned template engine.
Programming paradigms
D implements five main programming paradigms-imperative, OOP, meta-programming, functional programming, and parallel computing (actor model).
Memory management
D Use the garbage collector for memory management, but it is possible and manual control using operator overloading <code> new </ code> and <code> delete </ code>, as well as using malloc and free, similar to C. The garbage collector can be switched on and off manually, you can add or remove memory from his sight, forced to run a partial or complete assembly process. There is detailed management, which describes the different memory management schemes in D for the cases where the standard garbage collector is not applicable.
SafeD
SafeD - the name of the subset of D, which guarantees the use of security access memory.
Data types
The language has a rich set of certain types of data and tools for defining new types. Types of language D are divided into value types and types of links.
Base Types
A set of base types can be divided into the following categories:
- <Code> void </ code> - a special type of null values
- <Code> bool </ code> - boolean
- Integer types: signed <code> byte </ code>, <code> short </ code>, <code> int </ code>, <code> long </ code> and the corresponding unsigned <code> ubyte </ code>, <code> ushort </ code>, <code> uint </ code>, <code> ulong </ code>
- Types to floating point: <code> float </ code>, <code> double </ code>, <code> real </ code>. For floating-point types have their corresponding options for imaginary and complex numbers:
- Imaginary: <code> ifloat </ code>, <code> idouble </ code>, <code> ireal </ code>
- Complex: <code> sfloat </ code>, <code> sdouble </ code>, <code> sreal </ code>
- Sign (character) types: <code> char </ code>, <code> wchar </ code>, <code> dchar </ code>, indicating the code unit encodings of UTF-8, UTF-16 and UTF-32, respectively .
Unlike in C ++ all sizes of integer types specified. That is, an int is always 32 bits. Integer literals can be written in decimal, binary (prefix 0b) and hexadecimal (prefix 0x) notation. The recording method literals in octal in the style of C (ie with the prefix 0) has been removed, as such recording is easily confused with a decimal. If you still need to use octal, you can use the template 'std.conv.octal' .
Derived types
- <Code> pointer </ code> - Index
- <Code> array </ code> - Array
- <Code> associative array </ code> - an associative array
- <Code> function </ code> - function
- <Code> delegate </ code> - delegate
- <Code> string </ code>, <code> wstring </ code>, <code> dstring </ code> - convenient aliases for immutable arrays sign (character) type <code> immutable (char) [] </ code >, <code> immutable (wchar) [] </ code> and <code> immutable (dchar) [] </ code>, indicating the immutable (the qualifier 'immutable' ) line in one of the Unicode encodings UTF- 8, UTF-16 and UTF-32, respectively.
Custom Types
- <Code> alias </ code> - alias
- <Code> enum </ code> - listing
- <Code> struct </ code> - structure
- <Code> union </ code> - Association
- <Code> class </ code> - Class
Output types, the keywords "auto", "typeof" and unnamed ("Voldemort") types
In a mechanism in type inference. This means that the type can usually be computed at compile-time and it does not need to be explicit. For example, the expression: <code> auto myVar = 10 </ code> at compile time will be converted to <code> int myVar = 10 </ code>. Using type inference has several advantages:
- A more concise and readable code, especially if it uses long names of structures or classes. For example, the expression
<code> VeryLongTypeName var = VeryLongTypeName (/ * ... * /); </ code> It may be replaced by <code> auto var = VeryLongTypeName (/ * ... * /); </ code>
- With the keyword 'typeof' You can create a variable of the same type as that of an existing variable, even if its type is unknown. Example:
// file1.d
int var1;
// file2.d
typeof(var1) var2;
- the use of anonymous types. Example:
auto createVoldemortType(int value)
{
struct TheUnnameable
{
int getValue() { return value; }
}
return TheUnnameable();
}
Unnamed types officially called Voldemort-type similar to the Wollan de Mort ("He-Who-Must Not Call"), the main antagonist of the Harry Potter series. The output types are not to be confused with dynamic typing because although the type is not specified explicitly, it is evaluated at compile time rather than at run time.
Implementations
- DMD - Digital Mars D, the reference compiler developed by Walter Bright. This compiler implements the standard of the most complete language support for all the innovations appear in it in the first place. The front-end is licensed under the [[Boost Software License | Boost]], back-end - under a proprietary license with access to the source code. Part of the back-end code was developed at Symantec, and may not be re-licensed.
- GDC - DMD-front-end compiler GCC.
- LDC - DMD-front-end for LLVM
- SDC - Experimental compiler (compiler as a library), using LLVM as a backend and not based on the DMD.
Tools and development tools
IDE and Editors
Support in different IDE, implemented with the help of plug-ins:
IDE | Plug-in | Platform |
---|---|---|
Eclipse | DDT | cross-platform |
MonoDevelop / Xamarin | Mono-D | cross-platform |
Visual Studio | Visual-D | Windows |
XCode | D for Xcode | Mac OS X |
Zeus IDE | D for Zeus IDE | Windows |
Native language IDE for D - Coedit (Windows, Linux)
D supported in a variety of text editors: Vim, Emacs, Kate, Notepad++, Sublime Text, TextMate и других.
package Manager
DUB - the official package manager D. DUB serves as the repository of packages and used for dependency management, and as a build system. Set dependencies, metadata about the project and compiler flags are stored in the format JSON. An example of a simple project file:
{
"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"
}
}
Tools & Utilities
rdmd
dmd - a utility that comes bundled with the compiler DMD, allowing to compile and run files from the source D «on the fly." This allows you to use the D for small programs similar to bash and perl:
// myprog.d
#!/usr/bin/env rdmd
import std.stdio;
void main()
{
writeln("Hello, world with automated script running!");
}
Call the command <code> ./ myprog.d </ code> in the console will automatically compile and run the program.
DPaste
Dpaste - online service to run programs on D in the browser similar to the services and JSBin Codepen.
asm.dlang.org
asm.dlang.org - online compiler and assembler
Code samples
EXAMPLE 1
"Hello, world!"import std.stdio;
void main() {
writeln("Hello, world!");
}
EXAMPLE 2
The program, which takes the command line arguments, which was caused by theimport 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 line by line from a file and displays all the words that are anagrams of other wordsimport 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(" "));
}
}
}
Links
- ↑ "dmd front end now switched to Boost license". Retrieved September 9, 2014.
- ↑ "D 2.0 FAQ". Retrieved 11 August 2015.
- ↑ "Building assert() in Swift, Part 2: __FILE__ and __LINE__". Retrieved September 25, 2014.
Присоединяйся к команде
ISSN:
Следуй за Полисом
Оставайся в курсе последних событий
License
Except as otherwise noted, the content of this page is licensed under the Creative Commons Creative Commons «Attribution-NonCommercial-NoDerivatives» 4.0 License, and code samples are licensed under the Apache 2.0 License. See Terms of Use for details.