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

Book Description

Leverage the modern convenience and modelling power of the D programming language to develop software with native efficiency

About This Book

  • Acquire the skills to understand the fundamentals of D through its support for imperative and object-oriented programming

  • Take advantage of D’s powerful compile-time features, templates and ranges to apply generative, generic, and functional style

  • A systematic guide that will help you become familiar with the concepts in D with the help of simple and easy-to-understand examples

  • 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.

    What You Will Learn

  • Compile programs with DMD and manage projects with DUB

  • Work efficiently by binding your D programs with new and existing C libraries

  • Generate code at compile-time to enhance runtime performance

  • Implement complex templates for more powerful generic code

  • Write idiomatic D with range-based functional pipelines

  • Use the DUB repository to find a link with a variety of D libraries

  • Implement a web-app in D from the ground up

  • In Detail

    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.

    Style and approach

    A friendly guide to the D programming language and its ecosystem that walks programmers through all they need to know for a painless experience in learning D.

    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 code file.

    Table of Contents

    1. Learning D
      1. Table of Contents
      2. Learning D
      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. How to Get a D in Programming
        1. Say hello to D
          1. An introductory program
          2. Getting help
        2. The Digital Mars D compiler
          1. Frontends, backends, and linkers
          2. Installing DMD
            1. The Windows installer
            2. Installing from the ZIP
              1. The Windows ZIP
              2. The POSIX ZIPs
          3. Compiling the example
          4. Documentation and source code
            1. The documentation
            2. The source
        3. Say hello to MovieMan
          1. The problem
          2. The features
        4. DUB – the D build tool and package manager
          1. Getting started
          2. Configuring the MovieMan project
            1. Understanding dub.sdl
            2. Building and running MovieMan
            3. Changing the output directory
        5. Summary
      10. 2. Building a Foundation with D Fundamentals
        1. The very basics
          1. Identifiers
          2. A note about scope
          3. More on modules
            1. Module declarations
            2. More about import declarations
            3. The special package module
          4. Comments
          5. Variable declaration and initialization
        2. Basic types
          1. The types
          2. Literals
            1. Integer literals
            2. Floating-point literals
            3. Character literals
          3. Conversions
          4. Alias declarations
          5. Properties
        3. Basic operators
          1. Arithmetic operators
          2. Bitwise operators
          3. Relational and logical operators
          4. The cast operator
        4. Derived data types
          1. Pointers
          2. Arrays
            1. Array basics
            2. Rectangular arrays
            3. Slices
            4. Array literals
            5. Arrays and void
            6. Array operations
          3. Strings
            1. String essentials
            2. Alternative string literals
              1. WYSIWYG strings
              2. Delimited strings
              3. Token strings
          4. Associative arrays
        5. Control flow statements
          1. Traditional loops
          2. The foreach loop
          3. Traditional conditionals
          4. The goto statement
        6. Type qualifiers – const and immutable
          1. The contracts
          2. With the basic types
          3. With pointers
          4. With arrays
          5. Conversions
        7. Functions
          1. Overloaded functions
          2. ref and out
          3. inout parameters
          4. lazy parameters
          5. Function attributes
          6. Return statements and memory
          7. Function pointers and delegates
        8. MovieMan – first steps
          1. The io module
          2. The app module
        9. Summary
      11. 3. Programming Objects the D Way
        1. User-defined types
          1. Enumerations
          2. Unions
          3. Structs and classes
        2. Working with objects
          1. Encapsulation with protection attributes
            1. Public
            2. Private
            3. Package
            4. Voldemort types
          2. Constructors and destructors
            1. Class constructors and destructors
            2. Struct constructors and destructors
            3. Static constructors and destructors
          3. Inheritance and polymorphism
            1. Interfaces
            2. Fake inheritance
          4. Nested classes
        3. Objects with const and immutable
          1. const as a storage class
        4. Error handling
          1. Scope guards
          2. Exception handling
        5. Contract programming and unit tests
          1. Assert contracts
          2. Function contracts
          3. Invariants
          4. Unit tests
        6. MovieMan – adding menus
          1. The Menu base class
          2. The MainMenu class
          3. The DisplayMenu class
        7. Summary
      12. 4. Running Code at Compile Time
        1. Pragmas
          1. The lib pragma
          2. The msg pragma
          3. The inline pragma
        2. Conditional compilation
          1. The version condition
          2. The debug condition
          3. The static if condition
        3. Compile-time strings
          1. The import expression
          2. String mixins
        4. Compile-time function execution
        5. Odds and ends
          1. static assert
          2. The is expression
          3. Alignment
          4. Compile-time reflection
          5. User-defined attributes
        6. Summary
      13. 5. Generic Programming Made Easy
        1. Template basics
          1. Templates as code blocks
          2. Struct and class templates
          3. Enum templates
          4. Function templates
            1. Special features
          5. More template parameters
            1. Value parameters
            2. Alias parameters
            3. This parameters
        2. Beyond the basics
          1. Template specializations
            1. Specialization on pointers and arrays
          2. Template constraints
          3. Template mixins
          4. Variadic templates
            1. Terminology
            2. More on usage
        3. Operator overloading
          1. Non-templated operator overloads
            1. Comparison overloads – opEquals and opCmp
              1. opEquals
              2. opCmp
              3. Considerations
            2. Function call overloads
            3. Assignment overloads
            4. Index overloads
              1. opIndex
              2. opIndexAssign
              3. opDollar
          2. Templated operator overloads
            1. Unary overloads
            2. Binary overloads
            3. Cast overloads
            4. Operator assignment overloads
            5. Slice operator overloads
          3. Other overloads
            1. opDispatch
            2. opApply
            3. toHash
        4. MovieMan – the database
          1. db.d
          2. Back to the menus
        5. Summary
      14. 6. Understanding Ranges
        1. Ranges defined
          1. The problem
          2. The solution
            1. The interface
            2. A range for arrays
            3. The implementation of filter
            4. The test
          3. The real ranges
            1. Input ranges
            2. Forward ranges
            3. Bidirectional ranges
            4. Random-access ranges
            5. Output ranges
            6. Optional range primitives
              1. hasLength
              2. isInfinite
              3. Other options
        2. Ranges in use
          1. Custom ranges
            1. Getting a range from a stack
            2. A name generator range
            3. Other considerations
          2. Custom algorithms
        3. Summary
      15. 7. Composing Functional Pipelines with Algorithms and Ranges
        1. Functional programming and composable pipelines
          1. A simple example
          2. A more complex example
          3. Sometimes we can't
        2. Navigating Phobos
          1. std.range
            1. Generative ranges
              1. iota
              2. recurrence
              3. sequence
            2. Selective ranges
              1. take
              2. drop
              3. stride
              4. retro
            3. Compositional ranges
              1. chain
              2. roundRobin
              3. transposed
              4. zip
              5. lockstep
          2. std.algorithm
            1. Comparison
              1. equal
              2. cmp
              3. mismatch
              4. levenshteinDistance
            2. Iteration
              1. group
              2. map
              3. reduce
            3. Mutation
              1. copy
              2. fill
              3. remove
            4. Searching
              1. find
              2. count
              3. any
            5. Set operations
              1. setIntersection
              2. setDifference
              3. setUnion
            6. Sorting
              1. sort
              2. partition
          3. std.array
            1. Appender
            2. assocArray
            3. join
          4. Where to look for more
        3. MovieMan – wrapping up
          1. The db module
          2. The display menu
          3. Making it better
        4. Summary
      16. 8. Exploring the Wide World of D
        1. Online resources
          1. DWiki
          2. Planet D
          3. reddit and StackOverflow
          4. This Week in D
          5. DConf
          6. DSource
        2. Editors and IDEs
          1. Text editors
            1. Vim and Emacs
            2. Textadept
            3. Sublime Text
          2. IDEs
            1. Visual D
            2. Mono-D
            3. DDT
            4. Code::Blocks
        3. Tools and utilities
          1. DMD
            1. Optimized and debug builds
            2. Changing the default output
            3. Compiling libraries
            4. Using libraries
            5. Warnings
            6. Profiling
            7. Code coverage analysis
            8. Compile and run
          2. GDC and LDC
            1. GDC
            2. LDC
          3. RDMD
          4. DustMite
          5. DCD
          6. DVM
        4. Libraries
          1. code.dlang.org
            1. Using libraries from the DUB registry
            2. Registering libraries with the DUB registry
            3. Browsing the DUB registry
          2. Deimos and DerelictOrg
        5. Summary
      17. 9. Connecting D with C
        1. Preliminaries
          1. Terminology
            1. Bindings, wrappers, and ports
            2. Dynamic and static – context matters
              1. Static libraries and static linking
              2. Dynamic libraries and dynamic linking
              3. Dynamic and static bindings
          2. Object file formats
          3. Linkage attributes
            1. Name mangling
            2. Calling conventions
            3. Putting it together
        2. Binding D to C
          1. Function prototypes
            1. Manually loading shared libraries
            2. Trying it out
          2. C types to D types
            1. Strings and characters
            2. Special types
            3. Enumerations
            4. Structures
            5. Pointers
            6. Type aliases
            7. Function pointers
            8. Defined constants
            9. Function parameters and return types
            10. Symbols
            11. Global variables
            12. Macros
            13. Conditional compilation
        3. Calling C from D
          1. D arrays and C arrays
            1. Basic arrays
            2. Arrays of arrays
            3. Strings
          2. Memory
          3. C callbacks and exceptions
        4. Calling D from C
        5. Summary
      18. 10. Taking D Online
        1. The software
          1. vibe.d
            1. Package overview
            2. The anatomy of a vibe.d web app
          2. The database library
        2. MovieManWeb
          1. Getting started
          2. The basics of diet templates
            1. Tags and indentation
            2. Including and extending templates
            3. The MovieManWeb layout
          3. Setting up the database
          4. Fleshing out the index page
            1. Mapping web interface functions to URLs
            2. Rendering diet templates
            3. Rewriting index.dt
          5. Adding movies
            1. Implementing the addMovie function
            2. Implementing the postAdd function
            3. Implementing add.dt
            4. Modifying index.dt
            5. Modifying app.d
            6. Modifying layout.dt
          6. Listing movies
            1. Implementing the listMovies function
            2. Modifying the index function
            3. Modifying index.dt
          7. Finding movies
            1. Implementing the postFind function
            2. Implementing find.dt
            3. Modifying index.dt
            4. Modifying app.d and layout.dt
            5. Implementing the findMovie functions
              1. findMovie the first
              2. findMovie the second
          8. Editing and deleting movies
          9. Expanding on MovieManWeb
        3. Summary
      19. 11. Taking D to the Next Level
        1. Concurrency
          1. Threads and fibers
            1. Threads
            2. Fibers
          2. Data sharing
            1. __gshared
            2. Shared
          3. Synchronization and atomics
            1. Automatic synchronization
            2. Manual synchronization
            3. Atomics
          4. Message passing
          5. Parallelism
          6. More information
        2. SafeD
        3. Functional purity
        4. The garbage collector
        5. Connecting with C++
        6. More on Phobos
          1. std.container
          2. std.datetime
          3. std.digest
          4. std.experimental
          5. std.getopt
          6. std.process
          7. std.socket
          8. Modules for Unicode and other encodings
          9. System bindings
        7. Game development with D
        8. The future of D
        9. Summary
      20. Index