Programming with Types
Vlad Riscutia
  • MEAP began April 2019
  • Publication in November 2019 (estimated)
  • ISBN 9781617296413
  • 340 pages (estimated)
  • printed in black & white

This is by far one of the most extensive and well written books I've personally read on Types.

Justin Coulston
Type-related failures are common and can be very costly. Famously, in 1999, NASA’s Mars Climate Orbiter burned up in the atmosphere because of an error that could have easily been prevented with typing. By taking advantage of the strong type systems available in most modern programming languages, you can eliminate whole classes of errors. Programming with Types teaches you type system techniques for writing software that’s safe, correct, easy to test and maintain, and that practically documents itself. Master these techniques, and you may even help prevent an interstellar catastrophe!
Table of Contents detailed table of contents

1 Introduction to Typing

1.1 Who This Book Is For

1.2 Why Types Exist

1.2.1 0s and 1s

1.2.2 What are Types and Type Systems?

1.3 Benefits of Type Systems

1.3.1 Correctness

1.3.2 Immutability

1.3.3 Encapsulation

1.3.4 Composability

1.3.5 Readability

1.4 Types of Type Systems

1.4.1 Dynamic and Static Typing

1.4.2 Weak and Strong Typing

1.4.3 Type Inference

1.5 In This Book

1.6 Summary

2 Basic Types

2.1 Designing Functions That Don’t Return Values

2.1.1 The Empty Type

2.1.2 The Unit Type

2.1.3 Exercises

2.2 Boolean Logic and Short Circuits

2.2.1 Boolean Expressions

2.2.2 Short Circuit Evaluation

2.2.3 Exercise

2.3 Common Pitfalls of Numerical Types

2.3.1 Integer Types and Overflow

2.3.2 Floating-point Types and Rounding

2.3.3 Arbitrarily Large Numbers

2.3.4 Exercises

2.4 Encoding Text

2.4.1 Breaking Text

2.4.2 Encodings

2.4.3 Encoding Libraries

2.4.4 Exercises

2.5 Building Data Structures with Arrays and References

2.5.1 Fixed-Size Arrays

2.5.2 References

2.5.3 Efficient Lists

2.5.4 Binary Trees

2.5.5 Associative Arrays

2.5.6 Implementation Tradeoffs

2.5.7 Exercise

2.6 Summary

2.7 Answers to Exercises

3 Composition

3.1 Compound Types

3.1.1 Tuples

3.1.2 Assigning Meaning

3.1.3 Maintaining Invariants

3.1.4 Exercise

3.2 Expressing “Either-Or” with Types

3.2.1 Enumerations

3.2.2 Optional Types

3.2.3 Result or Error

3.2.4 Variants

3.2.5 Exercises

3.3 The Visitor Pattern

3.3.1 A Naïve Implementation

3.3.2 Using the Visitor Pattern

3.3.3 Visiting a Variant

3.3.4 Exercise

3.4 Algebraic Data Types

3.4.1 Product Types

3.4.2 Sum Types

3.4.3 Exercises

3.5 Summary

3.6 Answers to Exercises

4 Type Safety

4.1 Avoiding Primitive Obsession to Prevent Misinterpretation

4.1.1 The Mars Climate Orbiter

4.1.2 The Primitive Obsession Anti-Pattern

4.1.3 Exercise

4.2 Enforcing Constraints

4.2.1 Enforcing Constraints with The Constructor

4.2.2 Enforcing Constraints with a Factory

4.2.3 Exercise

4.3 Adding Type Information

4.3.1 Type Casting

4.3.2 Tracking Types Outside the Type System

4.3.3 Common Typecasts

4.3.4 Exercises

4.4 Hiding and Restoring Type Information

4.4.1 Heterogenous Collections

4.4.2 Serialization

4.4.3 Exercises

4.5 Summary

4.6 Answers to Exercises

5 Function Types

5.1 A Simple Strategy Pattern

5.1.1 A Functional Strategy

5.1.2 Typing Functions

5.1.3 Strategy Implementations

5.1.4 First-Class Functions

5.1.5 Exercises

5.2 A State Machine without Switch Statements

5.2.1 Early Programming with Types

5.2.2 State Machines

5.2.3 State Machine Implementation Recap

5.2.4 Exercises

5.3 Avoiding Expensive Computation with Lazy Values

5.3.1 Lambdas

5.3.2 Exercise

5.4 Map, Filter, Reduce

5.4.1 Map

5.4.2 Filter

5.4.3 Reduce

5.4.4 Library Support

5.4.5 Exercises

5.5 Functional Programming

5.6 Summary

5.7 Answers to Exercises

6 Advanced Applications of Function Types

6.1 A Simple Decorator Pattern

6.1.1 A Functional Decorator

6.1.2 Decorator Implementations

6.1.3 Closures

6.1.4 Exercises

6.2 Implementing a Counter

6.2.1 An Object-Oriented Counter

6.2.2 A Functional Counter

6.2.3 A Resumable Counter

6.2.4 Counter Implementations Recap

6.2.5 Exercises

6.3 Executing Long-Running Operations Asynchronously

6.3.1 Synchronous Execution

6.3.2 Asynchronous Execution: Callbacks

6.3.3 Asynchronous Execution Models

6.3.4 Asynchronous Functions Recap

6.3.5 Exercises

6.4 Simplifying Asynchronous Code

6.4.1 Chaining Promises

6.4.2 Creating Promises

6.4.3 More About Promises

6.4.4 async/await

6.4.5 Clean Asynchronous Code Recap

6.4.6 Exercises

6.5 Summary

6.6 Answers to Exercises

7 Subtyping

7.1 Distinguishing Between Similar Types in TypeScript

7.1.1 Structural and Nominal Subtyping Pros and Cons

7.1.2 Simulating Nominal Subtyping in TypeScript

7.1.3 Exercises

7.2 Assigning Anything to, Assigning to Anything

7.2.1 Safe Deserialization

7.2.2 Values for Error Cases

7.2.3 Top and Bottom Types Recap

7.2.4 Exercises

7.3 Allowed Substitutions

7.3.1 Subtyping and Sum Types

7.3.2 Subtyping and Collections

7.3.3 Subtyping and Function Return Types

7.3.4 Subtyping and Function Argument Types

7.3.5 Variance Recap

7.3.6 Exercises

7.4 Summary

7.5 Answers to Exercises

8 Elements of Object-Oriented Programming

8.1 Defining Contracts with Interfaces

8.1.1 Exercises

8.2 Inheriting Data and Behavior

8.2.1 The Is-A Rule of Thumb

8.2.2 Modeling a Hierarchy

8.2.3 Parameterizing Behavior of Expressions

8.2.4 Exercises

8.3 Composing Data and Behavior

8.3.1 The Has-A Rule of Thumb

8.3.2 Composite Classes

8.3.3 Implementing the Adapter Pattern

8.3.4 Exercises

8.4 Extending Data and Behavior

8.4.1 Extending Behavior with Composition

8.4.2 Extending Behavior with Mix-ins

8.4.3 Mix-in in TypeScript

8.4.4 Exercise

8.5 Alternatives to Purely Object-Oriented Code

8.5.1 Sum Types

8.5.2 Functional Programming

8.5.3 Generic Programming

8.6 Summary

8.7 Answers to Exercises

9 Generic Data Structures

9.1 Decoupling Concerns

9.1.1 A Reusable Identity Function

9.1.2 The Optional Type

9.1.3 Generic Types

9.1.4 Exercises

9.2 Generic Data Layout

9.2.1 Generic Data Structures

9.2.2 What Is a Data Structure?

9.2.3 Exercises

9.3 Traversing Any Data Structure

9.3.1 Using Iterators

9.3.2 Streamlining Iteration Code

9.3.3 Iterators Recap

9.3.4 Exercises

9.4 Streaming Data

9.4.1 Processing Pipelines

9.4.2 Exercises

9.5 Summary

9.6 Answers to Exercises

10 Generic Algorithms and Iterators

10.1 Better Map, Filter, Reduce

10.1.1 Map

10.1.2 Filter

10.1.3 Reduce

10.1.4 Filter/Reduce Pipeline

10.1.5 Exercises

10.2 Common Algorithms

10.2.1 Algorithms Instead of Loops

10.2.2 Implementing a Fluent Pipeline

10.2.3 Exercises

10.3 Constraining Type Parameters

10.3.1 Generic Data Structures with Type Constraints

10.3.2 Generic Algorithms with Type Constraints

10.3.3 Exercise

10.4 Efficient Reverse and Other Algorithms Using Iterators

10.4.1 Iterator Building Blocks

10.4.2 A Useful Find

10.4.3 An Efficient Reverse

10.4.4 Efficient Element Retrieval

10.4.5 Iterators Recap

10.4.6 Exercises

10.5 Adaptive Algorithms

10.5.1 Exercise

10.6 Summary

10.7 Answers to Exercises

11 Higher Kinded Types and Beyond

11.1 An Even More General Map

11.1.1 Processing Results or Propagating Errors

11.1.2 Mix-and-match Function Application

11.1.3 Functors and Higher Kinded Types

11.1.4 Functors for Functions

11.1.5 Exercise

11.2 Monads

11.2.1 Result or Error

11.2.2 Difference Between map() and bind()

11.2.3 The Monad Pattern

11.2.4 The Continuation Monad

11.2.5 The List Monad

11.2.6 Other Monads

11.2.7 Exercise

11.3 Where to Next

11.3.1 Functional Programming

11.3.2 Generic Programming

11.3.3 Higher Kinded Types and Category Theory

11.3.4 Dependent Types

11.3.5 Linear Types

11.4 Summary

11.5 Answers to Exercises


Appendix A: TypeScript Installation and Source Code

Appendix B: TypeScript Cheat Sheet

About the Technology

As software grows in complexity, it’s essential to adopt coding tools and strategies that enforce safety and correctness. A type system governs what types of data are allowable, requiring your code to accurately store, represent, and use its internal data. Recent efforts to bring type safety to dynamic languages like Python and JavaScript shine a light on the benefits—and the importance!—of type systems. Relying on the compiler to catch type mismatches ensures code will operate correctly at runtime. Type-aware programming also ensures data integrity, enables data encapsulation, and allows new components to be composed from existing ones.

About the book

Programming with Types teaches you to design safe, resilient, correct software that’s easy to maintain and understand by taking advantage of the power of strong type systems. Designed to provide practical, instantly-useful techniques for working developers, this clearly-written tutorial introduces you to using type systems to support everyday programming tasks. In it, you’ll explore the impact types have on functions, inheritance, and composition, and you’ll take a close look at generics, which enable greater abstraction. You’ll also discover folding data structures and applying functions to containers with higher kinded types. Along the way, you’ll explore real-world use cases and practical examples in TypeScript as you build your skills from primitive types up to more advanced concepts like functors and monads.

What's inside

  • An introduction to type systems and their benefits
  • Designing functions that don’t return values
  • Encoding text
  • Building data structures with arrays and references
  • Type composition
  • Function types
  • Type safety
  • Asynchronous functions
  • Subtyping
  • Object-oriented programming with types
  • Generic data structures and algorithms

About the reader

Readers need basic high school math skills as well as some programming experience in a mainstream programming language like Java, JavaScript, C#, or C++.

About the author

Vlad Riscutia is a principal software engineer at Microsoft with over 10 years of experience in the software field. He has headed up several major software projects and mentored many up-and-coming software engineers.

Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
MEAP combo $49.99 pBook + eBook + liveBook
MEAP eBook $39.99 pdf + ePub + kindle + liveBook
Prices displayed in rupees will be charged in USD when you check out.

placing your order...

Don't refresh or navigate away from the page.

FREE domestic shipping on three or more pBooks