Learn Type-Driven Development

Book Description

A fast paced guide for JavaScript developers for writing safe, fast, and reusable code by leveraging ResaonML's strong static type system

Key Features

  • Reduce code errors with the power of type systems
  • Employ static typechecking and genericity to promote code reuse and consistency
  • Understand functional programming which is the foundation of type-driven development

Book Description

Type-driven development is an approach that uses a static type system to achieve results including safety and efficiency. Types are used to express relationships and other assumptions directly in the code, and these assumptions are enforced by the compiler before the code is run. Learn Type-Driven Development covers how to use these type systems to check the logical consistency of your code.

This book begins with the basic idea behind type-driven development. You'll learn about values (or terms) and how they contrast with types. As you progress through the chapters, you'll cover how to combine types and values inside modules and build structured types out of simpler ones. You'll then understand how to express choices or alternatives directly in the type system using variants, polymorphic variants, and generalized algebraic data types. You'll also get to grips with sum types, build sophisticated data types from generics, and explore functions that express change in the types of values. In the concluding chapters, you'll cover advanced techniques for code reuse, such as parametric polymorphism and subtyping.

By end of this book, you will have learned how to iterate through a type-driven process of solving coding problems using static types, together with dynamic behavior, to obtain more safety and speed.

What you will learn

  • Use static types to capture information, making programs safer and faster
  • Learn ReasonML from experienced type-driven developers
  • Enhance safety by simply using basic types
  • Understand the most important type-driven concepts with simple examples
  • Explore a design space using static typing and find the best way to express your system rules
  • Use static types and dynamic runtime in harmony to write even safer and faster code

Who this book is for

If you're a programmer working with dynamically typed languages and are looking for ways to mitigate production runtime errors, Learn Type-Driven Development is for you. You'll also find this book helpful if you're a programmer working with statically typed languages looking for increased safety and improved performance.

Publisher Resources

Download Example Code

Table of Contents

  1. Title Page
  2. Copyright and Credits
    1. Learn Type-Driven Development
  3. Packt Upsell
    1. Why subscribe?
    2. Packt.com
  4. Contributors
    1. About the authors
    2. About the reviewer
    3. Packt is searching for authors like you
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  6. Starting Type-Driven Development
    1. Analyzing code for hidden errors
    2. Adding types
    3. ReasonML
      1. Why ReasonML?
      2. Getting started with ReasonML
      3. Using Try Reason
      4. Going further
    4. Summary
  7. Programming with Types and Values
    1. Workflow
    2. Types and values
      1. Static types
      2. What do you gain?
    3. Static and dynamic environments
      1. Values
        1. Scoping and shadowing
      2. Understanding type erasure
    4. Errors
      1. Syntax errors
      2. Type errors and inference
    5. Summary
  8. Packaging Types and Values Together
    1. File modules
    2. Syntactic modules
      1. Using a syntactic module
    3. Module signatures
      1. Exporting and documenting the public API
      2. Syntactic module signatures
    4. Module errors
      1. Signature mismatch
      2. Value can't be found
    5. Type abstraction
      1. Zero-allocation type abstraction
      2. Preventing type mix-ups
    6. Summary
  9. Grouping Values Together in Types
    1. Record types
      1. Record literals
        1. Accessing fields and dealing with errors
      2. Changing record values
        1. Immutable updates
        2. Mutable record fields
      3. Record patterns
    2. Tuple types
      1. Accessing tuple values
        1. Destructuring pattern matching
        2. Accessor functions
    3. Object types
      1. Inheritance and subtyping
    4. JavaScript object types
    5. Summary
  10. Putting Alternative Values in Types
    1. Variant types
      1. Pattern matching
    2. The power of switch
      1. Refutable patterns
        1. The when clause and general branching
      2. Stricter safety checks
    3. Polymorphic variant types
      1. Creating and typing values
      2. Inputting values into functions
      3. Understanding polymorphic variant inference
      4. Outputting values from functions
    4. Generalized algebraic data types
    5. Designing for correctness with types
      1. Products and sums together
      2. Recursive types
    6. Summary
  11. Making Types That Can Slot into Any Other Type
    1. Type inference and generic types
      1. The special case of interpolation
      2. Type parameters
    2. The list – modeling more than one
    3. The option – modeling either none or one
    4. Mutable parameterized types – ref and array
      1. Managing a reference to a value
      2. Managing an array of values
      3. Mutation and type inference restrictions
    5. Forcing a difference with phantom types
    6. Summary
  12. Making Types That Represent Operations
    1. Function types and other useful properties
      1. Referential transparency
      2. Function purity
      3. Totality
      4. What a function type means
    2. Multiple arguments and currying
      1. Functions as values
        1. Function literal syntax
        2. Eta abstraction
    3. Summary
  13. Reusing Code with Many Different Types
    1. Polymorphism in Reason
      1. Generic functions with parametric polymorphism
      2. Ad hoc polymorphism or overloading
    2. Generic code with functors
      1. What is a functor?
      2. Example 1 – looking in the standard library
      3. Example 2
      4. Example 3
    3. Summary
  14. Extending Types with New Behavior
    1. Subtyping using polymorphic variants
      1. Reusing constructors for different types
      2. An example of polymorphic variant type extension
      3. More about extending polymorphic variant types
    2. Code reuse with OOP-style inheritance
      1. Opening a module
      2. Including a module
    3. Summary
  15. Bringing It All Together
    1. Starting with a variant type (version 1)
    2. Using more pattern matching (version 2)
    3. Switching to polymorphic variant types (version 3)
    4. Using records (version 4)
    5. Using modules for code structure (version 5)
    6. An alternative code structure (version 6)
    7. An improvement – using lists as output (version 7)
    8. Another improvement – using mutable records (version 8)
    9. Unit testing our code (final version)
      1. Creating our final package and setting up for tests
      2. Writing our first tests
      3. Running the tests
    10. Summary
  16. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product Information

  • Title: Learn Type-Driven Development
  • Author(s): Yawar Amin, Kamon Ayeva
  • Release date: December 2018
  • Publisher(s): Packt Publishing
  • ISBN: 9781788838016