You are previewing D Cookbook.
O'Reilly logo
D Cookbook

Book Description

For experienced programmers this book is a journey into D, the language that can dramatically improve your productivity and capabilities. Learning is engaging and straightforward, thanks to the ‘How to do it’/’How it works’ approach.

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.

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
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. 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.

    Table of Contents

    1. D Cookbook
      1. Table of Contents
      2. D Cookbook
      3. Credits
      4. Foreword
      5. About the Author
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. 1. Core Tasks
        1. Introduction
        2. Installing the compiler and writing a "Hello World" program
          1. How to do it…
          2. How it works…
          3. There's more…
        3. Adding additional modules (files) to your program
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        4. Using external libraries
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        5. Building and processing arrays
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        6. Using associative arrays to translate input
          1. How to do it…
          2. How it works…
          3. There's more…
        7. Creating a user-defined vector type
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Using a custom exception type
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        9. Understanding immutability
          1. Getting ready
          2. How to do it…
            1. Writing functions
            2. Writing object methods
          3. How it works…
        10. Slicing a string to get a substring
          1. How to do it…
          2. How it works…
          3. There's more…
        11. Creating a tree of classes
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also…
      10. 2. Phobos – The Standard Library
        1. Introduction
        2. Performing type conversions
          1. How to do it…
          2. How it works…
          3. There's more…
        3. Finding the largest files in a directory
          1. How to do it…
          2. How it works…
        4. Creating a network client and server
          1. How to do it…
            1. Client
            2. Server
          2. How it works…
          3. There's more…
          4. See also
        5. Using Base64 to create a data URI
          1. How to do it…
          2. How it works…
        6. Generating random numbers
          1. How to do it…
          2. How it works…
          3. There's more…
        7. Normalizing a string and performing Unicode comparisons
          1. How to do it…
          2. How it works…
        8. Searching with regular expressions
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        9. Writing a digest utility
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        10. Using the std.zlib compression
          1. How to do it…
          2. How it works…
        11. Using the std.json module
          1. How to do it…
          2. How it works…
          3. See also
      11. 3. Ranges
        1. Introduction
        2. Using ranges when implementing an algorithm
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Creating an input range
          1. How to do it…
          2. How it works…
          3. There's more…
        4. Creating an output range
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        5. Creating a higher-order range
          1. How to do it…
          2. How it works…
        6. Putting a range interface on a collection
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Creating an input range over a tree structure
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Using runtime polymorphic (class) ranges
          1. How to do it…
          2. How it works…
        9. Storing a range as a data member
          1. How to do it…
          2. How it works…
        10. Sorting ranges efficiently
          1. Getting ready
          2. How to do it…
            1. To sort
            2. To benchmark
          3. How it works…
          4. See also
        11. Searching ranges
          1. How to do it…
          2. How it works…
        12. Using functional tools to query data
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
      12. 4. Integration
        1. Introduction
        2. Calling the Windows API functions
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Removing the Windows console
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        4. Making Linux system calls
          1. How to do it…
            1. With the C interface
            2. With inline assembly
          2. How it works…
          3. See also
        5. Writing part of a C program in D
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Interfacing with C++
          1. Getting ready
          2. How to do it …
          3. How to do it…
          4. See also
        7. Using structs to mimic the C++ object structure
          1. How to do it…
          2. How it works…
        8. Communicating with external processes
          1. How to do it…
          2. How it works…
          3. See also
        9. Communicating with a dynamic scripting language
          1. Getting ready
          2. How to do it…
          3. How it works…
        10. Using Windows' COM
          1. Getting ready
          2. How to do it…
            1. COM server
            2. COM client
          3. How it works…
          4. There's more…
      13. 5. Resource Management
        1. Introduction
        2. Avoiding the garbage collector
          1. How to do it…
          2. How it works…
        3. Making a reference-counted object
          1. How to do it…
          2. How it works…
          3. See also
        4. Manually managing class memory
          1. How to do it…
          2. How it works…
          3. See also
        5. Using scope guards to manage transactions
          1. How to do it…
          2. How it works…
        6. Creating an array replacement
          1. How to do it…
          2. How it works…
        7. Managing lent resources
          1. How to do it…
          2. How it works…
        8. Creating a NotNull struct
          1. How to do it…
          2. How it works…
          3. There's more…
        9. Using unique pointers
          1. How to do it…
          2. How it works…
        10. Using RAII and handling the limitations of class destructors
          1. How to do it…
          2. How it works…
      14. 6. Wrapped Types
        1. Introduction
        2. Creating a struct with reference semantics
          1. How to do it…
          2. How it works…
          3. There's more…
        3. Simulating inheritance with structs
          1. How to do it…
          2. How it works…
        4. Creating a ranged integer
          1. How to do it…
          2. How it works…
          3. There's more…
        5. Creating an opaque handle type
          1. How to do it…
          2. How it works…
          3. There's more…
        6. Creating a subtyped string for i18n
          1. How to do it…
          2. How it works…
        7. Forwarding methods with opDispatch
          1. How to do it…
          2. How it works…
        8. Creating a tagged dynamic type
          1. How to do it…
          2. How it works…
          3. There's more…
        9. Creating a structure with two views into the same data
          1. How to do it…
          2. How it works…
        10. Simulating multiple inheritance with mixin templates
          1. How to do it…
          2. How it works…
      15. 7. Correctness Checking
        1. Introduction
        2. Using assertions and exceptions
          1. How to do it…
          2. How it works…
          3. See also
        3. Using static asserts
          1. How to do it…
          2. How it works…
        4. Using template constraints and static if
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Preventing memory corruption bugs with @safe
          1. How to do it…
          2. How it works…
          3. See also
        6. Leveraging const-correctness
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Avoiding side effects of pure functions
          1. How to do it…
          2. How it works…
        8. Verifying object invariants and pre- and post-conditions
          1. How to do it…
          2. How it works…
        9. Unit testing your code
          1. How to do it…
          2. How it works…
          3. See also
        10. Documenting your code with Ddoc
          1. How to do it…
          2. How it works…
          3. There's more…
        11. Writing platform-specific code (versions) and per-client configuration modules
          1. How to do it…
            1. Platform-specific code
            2. Client-specific code
          2. How it works…
          3. See also
      16. 8. Reflection
        1. Introduction
        2. Getting dynamic runtime type information
          1. How to do it…
          2. How it works…
          3. See also
        3. Getting a list of child classes
          1. How to do it…
          2. How it works…
        4. Determining whether a module is available
          1. How to do it…
          2. How it works…
          3. See also
        5. Getting a list of all methods or fields in a module or an object
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        6. Inspecting function overloads
          1. How to do it…
          2. How it works…
        7. Determining names, types, and default values of function parameters
          1. How to do it…
          2. How it works…
          3. There's more…
        8. Getting components of complex types
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also:
        9. Using user-defined attributes
          1. How to do it…
          2. How it works…
        10. Implementing a custom lint-style check for virtual functions
          1. How to do it…
          2. How it works…
        11. Extending the runtime type information
          1. How to do it…
          2. How it works…
          3. There's more…
        12. Creating a command-line function caller
          1. How to do it…
          2. How it works…
          3. There's more…
      17. 9. Code Generation
        1. Introduction
        2. Creating user-defined literals
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Parsing a domain-specific language
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        4. Generating data structures from text diagrams
          1. How to do it…
          2. How it works…
          3. See also
        5. Automating dynamic calls with multiple dispatch
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Building a lookup table
          1. How to do it…
          2. How it works…
        7. Using string parameters to change functions
          1. How to do it…
          2. How it works…
        8. Wrapping instance methods
          1. How to do it…
          2. How it works…
          3. See also
        9. Using opDispatch to generate properties
          1. How to do it…
          2. How it works…
        10. Duck typing to a statically-defined interface
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      18. 10. Multitasking
        1. Introduction
        2. Using threads
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        3. Passing messages with std.concurrency
          1. How to do it…
          2. How it works…
          3. See also
        4. Processing parallel data with std.parallelism
          1. How to do it…
          2. How it works…
          3. See also
        5. Using fibers
          1. How to do it…
          2. How it works…
          3. See also
        6. Creating new processes
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        7. Exploring thread-safe, single-locking singletons
          1. How to do it…
          2. How it works…
          3. See also
        8. Using asynchronous I/O
          1. How to do it…
          2. How it works…
          3. See also
      19. 11. D for Kernel Coding
        1. Introduction
        2. Running D on bare metal x86 with a stripped runtime
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Adding interrupt handling to the bare metal x86 code
          1. Getting ready
          2. How to do it…
          3. How it works…
      20. 12. Web and GUI Programming
        1. Introduction
        2. Creating a dynamic website with cgi.d
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Creating a web API with web.d
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Parsing and modifying an HTML page with dom.d
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Accessing a SQL database
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. Sending an e-mail
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Writing colored output to the console
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Getting real-time input from the terminal
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        9. Working with image files
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        10. Creating a graphics window to show a TV static demo
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        11. Creating an OpenGL window
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      21. A. Addendum
        1. Compiling D for ARM/Linux Raspberry Pi
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        2. Running D on bare metal ARM
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Using the exponentiation operator
          1. How to do it…
          2. How it works…
        4. Getting a stack trace without throwing an exception
          1. How to do it…
          2. How it works…
        5. Finding more information about D
          1. How to do it…
          2. How it works…
      22. Index