formatting
This commit is contained in:
parent
68c048ef5c
commit
0f621a573d
1 changed files with 86 additions and 82 deletions
|
@ -1,81 +1,81 @@
|
||||||
# Hands-On DLang
|
# Hands-On DLang
|
||||||
|
|
||||||
- [Hands-On DLang](#hands-on-dlang)
|
* [Hands-On DLang](#hands-on-dlang)
|
||||||
- [Setup](#setup)
|
* [Setup](#setup)
|
||||||
- [Installing DMD and DUB](#installing-dmd-and-dub)
|
* [Installing DMD and DUB](#installing-dmd-and-dub)
|
||||||
- [OS X](#os-x)
|
* [OS X](#os-x)
|
||||||
- [Installing with Homebrew (recommended)](#installing-with-homebrew-recommended)
|
* [Installing with Homebrew (recommended)](#installing-with-homebrew-recommended)
|
||||||
- [Installing locally using the install script](#installing-locally-using-the-install-script)
|
* [Installing locally using the install script](#installing-locally-using-the-install-script)
|
||||||
- [Installing using the installer](#installing-using-the-installer)
|
* [Installing using the installer](#installing-using-the-installer)
|
||||||
- [Windows](#windows)
|
* [Windows](#windows)
|
||||||
- [Recommended editor setup](#recommended-editor-setup)
|
* [Recommended editor setup](#recommended-editor-setup)
|
||||||
- [Installation of Visual Studio Code](#installation-of-visual-studio-code)
|
* [Installation of Visual Studio Code](#installation-of-visual-studio-code)
|
||||||
- [Extension setup](#extension-setup)
|
* [Extension setup](#extension-setup)
|
||||||
- [Basics](#basics)
|
* [Basics](#basics)
|
||||||
- [Hello World](#hello-world)
|
* [Hello World](#hello-world)
|
||||||
- [Imports and modules](#imports-and-modules)
|
* [Imports and modules](#imports-and-modules)
|
||||||
- [Selective imports](#selective-imports)
|
* [Selective imports](#selective-imports)
|
||||||
- [Scoped imports](#scoped-imports)
|
* [Scoped imports](#scoped-imports)
|
||||||
- [Imports match files and directories](#imports-match-files-and-directories)
|
* [Imports match files and directories](#imports-match-files-and-directories)
|
||||||
- [Basic Types](#basic-types)
|
* [Basic Types](#basic-types)
|
||||||
- [Type conversion](#type-conversion)
|
* [Type conversion](#type-conversion)
|
||||||
- [Type properties](#type-properties)
|
* [Type properties](#type-properties)
|
||||||
- [Indexing](#indexing)
|
* [Indexing](#indexing)
|
||||||
- [Variable declarations](#variable-declarations)
|
* [Variable declarations](#variable-declarations)
|
||||||
- [Mutability](#mutability)
|
* [Mutability](#mutability)
|
||||||
- [`immutable`](#immutable)
|
* [`immutable`](#immutable)
|
||||||
- [`const`](#const)
|
* [`const`](#const)
|
||||||
- [Functions](#functions)
|
* [Functions](#functions)
|
||||||
- [Return type deduction](#return-type-deduction)
|
* [Return type deduction](#return-type-deduction)
|
||||||
- [Default arguments](#default-arguments)
|
* [Default arguments](#default-arguments)
|
||||||
- [Local functions](#local-functions)
|
* [Local functions](#local-functions)
|
||||||
- [Memory and pointers](#memory-and-pointers)
|
* [Memory and pointers](#memory-and-pointers)
|
||||||
- [Memory safety](#memory-safety)
|
* [Memory safety](#memory-safety)
|
||||||
- [Structs](#structs)
|
* [Structs](#structs)
|
||||||
- [Member functions](#member-functions)
|
* [Member functions](#member-functions)
|
||||||
- [`const` member functions](#const-member-functions)
|
* [`const` member functions](#const-member-functions)
|
||||||
- [`static` member functions](#static-member-functions)
|
* [`static` member functions](#static-member-functions)
|
||||||
- [Arrays](#arrays)
|
* [Arrays](#arrays)
|
||||||
- [Static arrays](#static-arrays)
|
* [Static arrays](#static-arrays)
|
||||||
- [Dynamic arrays](#dynamic-arrays)
|
* [Dynamic arrays](#dynamic-arrays)
|
||||||
- [Array operations and properties](#array-operations-and-properties)
|
* [Array operations and properties](#array-operations-and-properties)
|
||||||
- [Slices](#slices)
|
* [Slices](#slices)
|
||||||
- [Alias and `string`s](#alias-and-strings)
|
* [Alias and `string`s](#alias-and-strings)
|
||||||
- [Control flow](#control-flow)
|
* [Control flow](#control-flow)
|
||||||
- [if…else](#if%E2%80%A6else)
|
* [if…else](#if%E2%80%A6else)
|
||||||
- [switch…case](#switch%E2%80%A6case)
|
* [switch…case](#switch%E2%80%A6case)
|
||||||
- [Old fashioned loops](#old-fashioned-loops)
|
* [Old fashioned loops](#old-fashioned-loops)
|
||||||
- [Breaking out of outer loops](#breaking-out-of-outer-loops)
|
* [Breaking out of outer loops](#breaking-out-of-outer-loops)
|
||||||
- [`foreach` loops](#foreach-loops)
|
* [`foreach` loops](#foreach-loops)
|
||||||
- [Element iteration](#element-iteration)
|
* [Element iteration](#element-iteration)
|
||||||
- [Access by reference](#access-by-reference)
|
* [Access by reference](#access-by-reference)
|
||||||
- [Iterate `n` times](#iterate-n-times)
|
* [Iterate `n` times](#iterate-n-times)
|
||||||
- [Iteration with index counter](#iteration-with-index-counter)
|
* [Iteration with index counter](#iteration-with-index-counter)
|
||||||
- [Ranges](#ranges)
|
* [Ranges](#ranges)
|
||||||
- [Laziness](#laziness)
|
* [Laziness](#laziness)
|
||||||
- [Copying ranges](#copying-ranges)
|
* [Copying ranges](#copying-ranges)
|
||||||
- [`RandomAccessRange`s](#randomaccessranges)
|
* [`RandomAccessRange`s](#randomaccessranges)
|
||||||
- [Lazy range algorithms](#lazy-range-algorithms)
|
* [Lazy range algorithms](#lazy-range-algorithms)
|
||||||
- [Associative arrays](#associative-arrays)
|
* [Associative arrays](#associative-arrays)
|
||||||
- [Classes](#classes)
|
* [Classes](#classes)
|
||||||
- [Inheritance](#inheritance)
|
* [Inheritance](#inheritance)
|
||||||
- [Final and abstract member functions](#final-and-abstract-member-functions)
|
* [Final and abstract member functions](#final-and-abstract-member-functions)
|
||||||
- [Checking for identity](#checking-for-identity)
|
* [Checking for identity](#checking-for-identity)
|
||||||
- [Interfaces](#interfaces)
|
* [Interfaces](#interfaces)
|
||||||
- [Templates](#templates)
|
* [Templates](#templates)
|
||||||
- [Template functions](#template-functions)
|
* [Template functions](#template-functions)
|
||||||
- [Other templates](#other-templates)
|
* [Other templates](#other-templates)
|
||||||
- [Template value parameters](#template-value-parameters)
|
* [Template value parameters](#template-value-parameters)
|
||||||
- [Other template parameters](#other-template-parameters)
|
* [Other template parameters](#other-template-parameters)
|
||||||
- [Delegates](#delegates)
|
* [Delegates](#delegates)
|
||||||
- [Functions as arguments](#functions-as-arguments)
|
* [Functions as arguments](#functions-as-arguments)
|
||||||
- [Local functions with context](#local-functions-with-context)
|
* [Local functions with context](#local-functions-with-context)
|
||||||
- [Anonymous functions and lambdas](#anonymous-functions-and-lambdas)
|
* [Anonymous functions and lambdas](#anonymous-functions-and-lambdas)
|
||||||
- [Exceptions](#exceptions)
|
* [Exceptions](#exceptions)
|
||||||
- [`nothrow`](#nothrow)
|
* [`nothrow`](#nothrow)
|
||||||
- [Gems](#gems)
|
* [Gems](#gems)
|
||||||
- [Uniform function call syntax (UFCS)](#uniform-function-call-syntax-ufcs)
|
* [Uniform function call syntax (UFCS)](#uniform-function-call-syntax-ufcs)
|
||||||
- [Scope guards](#scope-guards)
|
* [Scope guards](#scope-guards)
|
||||||
|
|
||||||
## Setup
|
## Setup
|
||||||
|
|
||||||
|
@ -875,6 +875,7 @@ if (c is null) // ok
|
||||||
### Interfaces
|
### Interfaces
|
||||||
|
|
||||||
`interface`s work basically the same as in Java:
|
`interface`s work basically the same as in Java:
|
||||||
|
|
||||||
```D
|
```D
|
||||||
interface Animal {
|
interface Animal {
|
||||||
void makeNoise();
|
void makeNoise();
|
||||||
|
@ -894,6 +895,7 @@ animal.makeNoise();
|
||||||
### Templates
|
### Templates
|
||||||
|
|
||||||
#### Template functions
|
#### Template functions
|
||||||
|
|
||||||
Template functions in D are very similar to C++ template functions:
|
Template functions in D are very similar to C++ template functions:
|
||||||
|
|
||||||
```D
|
```D
|
||||||
|
@ -917,7 +919,6 @@ template add(T) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
D also allows implicit function template instanciation:
|
D also allows implicit function template instanciation:
|
||||||
|
|
||||||
```D
|
```D
|
||||||
|
@ -938,6 +939,7 @@ print!(int, string)(42, "is the best number");
|
||||||
#### Other templates
|
#### Other templates
|
||||||
|
|
||||||
Of course, `struct`s, `class`es and `interface`s can also be templated:
|
Of course, `struct`s, `class`es and `interface`s can also be templated:
|
||||||
|
|
||||||
```D
|
```D
|
||||||
struct S(T) {
|
struct S(T) {
|
||||||
/* … */
|
/* … */
|
||||||
|
@ -982,12 +984,11 @@ dog.makeNoise(); // woof!
|
||||||
|
|
||||||
#### Other template parameters
|
#### Other template parameters
|
||||||
|
|
||||||
There are some other types of template parameters: template alias parameters
|
There are some other types of template parameters: template alias parameters
|
||||||
and template sequence parameters. Template alias parameters can be any D symbol
|
and template sequence parameters. Template alias parameters can be any D symbol
|
||||||
(which means basically anything except for basic types):
|
(which means basically anything except for basic types):
|
||||||
|
|
||||||
```D
|
```D
|
||||||
|
|
||||||
void print(alias var)() {
|
void print(alias var)() {
|
||||||
writeln(var);
|
writeln(var);
|
||||||
}
|
}
|
||||||
|
@ -1052,6 +1053,7 @@ writeln(f()); // 42
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Anonymous functions and lambdas
|
#### Anonymous functions and lambdas
|
||||||
|
|
||||||
You can write anonymous functions and lambdas like this:
|
You can write anonymous functions and lambdas like this:
|
||||||
|
|
||||||
```D
|
```D
|
||||||
|
@ -1069,6 +1071,7 @@ These are often passed as template arguments in the functional parts of Phobos:
|
||||||
```
|
```
|
||||||
|
|
||||||
### Exceptions
|
### Exceptions
|
||||||
|
|
||||||
Exceptions in D are very similar to Exceptions in Java:
|
Exceptions in D are very similar to Exceptions in Java:
|
||||||
|
|
||||||
```D
|
```D
|
||||||
|
@ -1108,7 +1111,6 @@ int divide(int a, int b) {
|
||||||
int divide4By2() nothrow {
|
int divide4By2() nothrow {
|
||||||
return divide(4, 2); // error, divide my throw
|
return divide(4, 2); // error, divide my throw
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Gems
|
## Gems
|
||||||
|
@ -1142,7 +1144,7 @@ auto a = "Cool Stuff".toLower;
|
||||||
assert(a == "cool stuff");
|
assert(a == "cool stuff");
|
||||||
```
|
```
|
||||||
|
|
||||||
UFCS is especially important when dealing with ranges where several algorithms can be put together to perform complex operations, still allowing to write clear and manageable code.
|
UFCS is especially important when dealing with ranges where several algorithms can be put together to perform complex operations, still allowing to write clear and manageable code.
|
||||||
|
|
||||||
```D
|
```D
|
||||||
import std.algorithm : group;
|
import std.algorithm : group;
|
||||||
|
@ -1154,6 +1156,7 @@ import std.range : chain, retro, front, retro;
|
||||||
### Scope guards
|
### Scope guards
|
||||||
|
|
||||||
Scope guards allow executing statements at certain conditions if the current block is left:
|
Scope guards allow executing statements at certain conditions if the current block is left:
|
||||||
|
|
||||||
* `scope(exit)` will always call the statements.
|
* `scope(exit)` will always call the statements.
|
||||||
* `scope(success)` statements are called when no exceptions have been thrown.
|
* `scope(success)` statements are called when no exceptions have been thrown.
|
||||||
* `scope(failure)` denotes statements that will be called when an exception
|
* `scope(failure)` denotes statements that will be called when an exception
|
||||||
|
@ -1170,4 +1173,5 @@ void foo() {
|
||||||
scope(exit) free(p);
|
scope(exit) free(p);
|
||||||
/* Do some stuff, which potentially might throw, which does not matter,
|
/* Do some stuff, which potentially might throw, which does not matter,
|
||||||
p is freed anyways when leaving the scope */
|
p is freed anyways when leaving the scope */
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|
Loading…
Reference in a new issue