Book Image

Learning D

By : Michael Parker
Book Image

Learning D

By: Michael Parker

Overview of this book

D is a modern programming language that is both powerful and efficient. It combines multiple paradigms in a way that opens up a whole new world of software design. It is used to develop both desktop and web applications, with future targets including mobile, and is available on multiple platforms. It is familiar to anyone with some experience in one or more of the C-family languages. However, hidden in the similarities are several differences that can be surprising when trying to apply common idioms from other languages. When learning D on your own, this can make it more time-consuming to master. In order to make the most of the language and become an idiomatic D programmer, it’s necessary to learn how to think in D. This book familiarizes you with D from the ground up, with a heavy focus on helping you to avoid surprises so that you can take your D knowledge to the next level more quickly and painlessly. Your journey begins with a taste of the language and the basics of compiling D programs with DMD, the reference D compiler developed by Digital Mars, and DUB, a community-developed build utility and package manager. You then set out on an exploration of major language features. This begins with the fundamentals of D, including built-in types, conditionals, loops and all of the basic building-blocks of a D program, followed by an examination of D’s object-oriented programming support. You’ll learn how these features differ from languages you may already be familiar with. Next up are D’s compile-time features, such as Compile-Time Function Evaluation and conditional compilation, then generic programming with templates. After that, you’ll learn the more advanced features of ranges and functional pipeline programming. To enhance your D experience, you are next taken on a tour of the D ecosystem and learn how to make D interact with C. Finally, you get a look at D web development using the vibe.d project and the book closes with some handy advice on where to go next.
Table of Contents (19 chapters)
Learning D
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Walter Bright first released the D programming language into the wild on December 8, 2001. Three weeks later, seven more iterations of the compiler had been uploaded to the Digital Mars website, incorporating fixes for bugs reported by users who had already begun experimenting with this exciting new language. In the years since, enthusiasts have continued to actively participate in D's development, pushing the language through two major versions and numerous compiler releases. D is very much a community-driven programming language.

This book aims to bring you up to speed with D to the degree that you can be confident in developing your own D programs and, if you are so motivated, participate in activities that drive the language forward. It is assumed that you already have some familiarity with other languages similar to D, such as C++ or Java, and have some familiarity with working with the command line. With this in mind, fewer details will be given for the features of D that are similar to those of other C-family languages and no instructions will be given on how to perform basic command-line tasks, such as changing directories or setting the system path.

What this book covers

Chapter 1, How to Get a D in Programming, introduces you to the D programming language and provides instructions for setting up the DMD compiler and the DUB build tool and package manager.

Chapter 2, Building a Foundation with D Fundamentals, gives an overview of all of D's foundational features, such as basic types, loop constructs, flow control, and more.

Chapter 3, Programming Objects the D Way, discusses D's support for object-oriented programming, including aggregate types and interfaces.

Chapter 4, Running Code at Compile Time, provides a tutorial on the compile-time aspects of D, including its support for Generative Programming and CTFE (Compile-Time Function Evaluation).

Chapter 5, Generic Programming Made Easy, explores the basics of D's support for Generic Programming, including templates, template constraints, and mixins.

Chapter 6, Understanding Ranges, introduces the Range concept, which serves as the core of D's support for functional programming.

Chapter 7, Composing Functional Pipelines with Algorithms and Ranges, explores several range-based functions in the standard library that can be used to write functional-style code and reduce memory allocations.

Chapter 8, Exploring the Wide World of D, looks at the D ecosystem, highlighting specific websites, tools, and third-party libraries.

Chapter 9, Connecting D with C, references how to create D bindings for C libraries to take advantage of existing codebases.

Chapter 10, Taking D Online, introduces the asynchronous, event-driven networking and web app framework, vibe.d, through the development of a sample project.

Chapter 11, Taking D to the Next Level, provides a quick look at other language and library features that can serve as a starting point for further exploration of the D programming language.

What you need for this book

To compile the code examples in this book, you will need DMD 2.068 or a later version. To compile the sample projects, you will also need DUB 0.9.24 or a later version. Installation instructions for both are provided in the first chapter.

In order to download dependencies, the color example in Chapter 8, Exploring the Wide World of D, and the sample project in Chapter 10, Taking D Online, requires an Internet connection the first time they are compiled.

Who this book is for

This book is intended for those with some background in a C-family language who want to learn how to apply their knowledge and experience to D. Perhaps you're a college student looking to use D for hobby projects, or a career programmer interested in expanding your skillset. This book will help you get up to speed with the language and avoid common pitfalls that arise when translating C-family experience to D.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "An anonymous enum declaration does not declare a new type."

A block of code is set as follows:

void healthBasedSwap(int[] squad1, int[] squad2) {
  import std.algorithm : sort, SwapStrategy;
  import std.range : chain;
  squad1.chain(squad2).sort!((a,b) => a > b,
  SwapStrategy.unstable)();
} 

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

void healthBasedSwap(int[] squad1, int[] squad2) {
  import std.algorithm : sort, SwapStrategy;
  import std.range : chain;
  squad1.chain(squad2).sort!((a,b) => a > b,
  SwapStrategy.unstable)();
} 

Any command-line input or output is written as follows:

dmd hello.d

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.