Free Sample
+ Collection

D Cookbook

Adam D. Ruppe

Discover the advantages of programming in D with over 100 incredibly effective recipes with this book and ebook.
RRP $29.99
RRP $49.99
Print + eBook

Want this title & more?

$12.99 p/month

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781783287215
Paperback362 pages

About This Book

  • Leverage D to write efficient and correct programs with minimum code
  • Learn advanced code generation techniques to automate programming tasks
  • See how to apply D idioms to real-world problems and understand how it can benefit you

Who This Book Is For

If you are an experienced programmer who is looking to explore a language that offers plenty of advantages over more established programming languages, this is the book for you. We assume that you are already familiar with general programming language basics, but you do not need to be a proficient user of D.

Table of Contents

Chapter 1: Core Tasks
Installing the compiler and writing a "Hello World" program
Adding additional modules (files) to your program
Using external libraries
Building and processing arrays
Using associative arrays to translate input
Creating a user-defined vector type
Using a custom exception type
Understanding immutability
Slicing a string to get a substring
Creating a tree of classes
Chapter 2: Phobos – The Standard Library
Performing type conversions
Finding the largest files in a directory
Creating a network client and server
Using Base64 to create a data URI
Generating random numbers
Normalizing a string and performing Unicode comparisons
Searching with regular expressions
Writing a digest utility
Using the std.zlib compression
Using the std.json module
Chapter 3: Ranges
Using ranges when implementing an algorithm
Creating an input range
Creating an output range
Creating a higher-order range
Putting a range interface on a collection
Creating an input range over a tree structure
Using runtime polymorphic (class) ranges
Storing a range as a data member
Sorting ranges efficiently
Searching ranges
Using functional tools to query data
Chapter 4: Integration
Calling the Windows API functions
Removing the Windows console
Making Linux system calls
Writing part of a C program in D
Interfacing with C++
Using structs to mimic the C++ object structure
Communicating with external processes
Communicating with a dynamic scripting language
Using Windows' COM
Chapter 5: Resource Management
Avoiding the garbage collector
Making a reference-counted object
Manually managing class memory
Using scope guards to manage transactions
Creating an array replacement
Managing lent resources
Creating a NotNull struct
Using unique pointers
Using RAII and handling the limitations of class destructors
Chapter 6: Wrapped Types
Creating a struct with reference semantics
Simulating inheritance with structs
Creating a ranged integer
Creating an opaque handle type
Creating a subtyped string for i18n
Forwarding methods with opDispatch
Creating a tagged dynamic type
Creating a structure with two views into the same data
Simulating multiple inheritance with mixin templates
Chapter 7: Correctness Checking
Using assertions and exceptions
Using static asserts
Using template constraints and static if
Preventing memory corruption bugs with @safe
Leveraging const-correctness
Avoiding side effects of pure functions
Verifying object invariants and pre- and post-conditions
Unit testing your code
Documenting your code with Ddoc
Writing platform-specific code (versions) and per-client configuration modules
Chapter 8: Reflection
Getting dynamic runtime type information
Getting a list of child classes
Determining whether a module is available
Getting a list of all methods or fields in a module or an object
Inspecting function overloads
Determining names, types, and default values of function parameters
Getting components of complex types
Using user-defined attributes
Implementing a custom lint-style check for virtual functions
Extending the runtime type information
Creating a command-line function caller
Chapter 9: Code Generation
Creating user-defined literals
Parsing a domain-specific language
Generating data structures from text diagrams
Automating dynamic calls with multiple dispatch
Building a lookup table
Using string parameters to change functions
Wrapping instance methods
Using opDispatch to generate properties
Duck typing to a statically-defined interface
Chapter 10: Multitasking
Using threads
Passing messages with std.concurrency
Processing parallel data with std.parallelism
Using fibers
Creating new processes
Exploring thread-safe, single-locking singletons
Using asynchronous I/O
Chapter 11: D for Kernel Coding
Running D on bare metal x86 with a stripped runtime
Adding interrupt handling to the bare metal x86 code
Chapter 12: Web and GUI Programming
Creating a dynamic website with cgi.d
Creating a web API with web.d
Parsing and modifying an HTML page with dom.d
Accessing a SQL database
Sending an e-mail
Writing colored output to the console
Getting real-time input from the terminal
Working with image files
Creating a graphics window to show a TV static demo
Creating an OpenGL window

What You Will Learn

  • Use the D programming language from "Hello World" to bare metal code
  • Gain an insight into avoiding the garbage collector and understand its pros and cons in real-world code examples
  • Discover how to use D's abstraction abilities to make the most of its effi cient and easy resource management capabilities
  • Implement and use ranges, a D idiom for lazy sequence implementation with decoupled generic algorithms
  • Explore a variety of third-party libraries available to get your work done faster
  • Integrate D into existing C, C++, and other environments
  • Prove program correctness with static analysis and test-driven development

In Detail

D is a modern programming language that uses the familiar C family syntax while offering advanced modeling capabilities, safety guarantees, programmer productivity, and high efficiency. It helps you to get the most out of your hardware and your programmers simultaneously, saving both development and deployment costs.

This practical guide will walk you through getting the work done with D, from writing your first program to writing advanced autogenerated objects, with notes based on real-world experiences telling you about potential pitfalls and how to avoid them. You'll use some of the third-party libraries available for D to get code working fast, including access to database engines, image processing, and more.


Read More