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