Book Image

Learn Type-Driven Development

By : Yawar Amin, Kamon Ayeva
Book Image

Learn Type-Driven Development

By: Yawar Amin, Kamon Ayeva

Overview of this book

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.
Table of Contents (12 chapters)

Type inference and generic types

Let's look at a few interesting examples of Reason's type inference and how it decides what types need to be filled in later, as illustrated in the following snippet:

/* src/Ch06/Ch06_GenericInference.re */
let triple(x) = (x, x, x); /* (1) */
let wrap(x) = `wrap(x); /* (2) */
let makeObj(x) = {as _; pub x = x}; /* (3) */
let greet(x) = print_endline({j|Hello, $x!|j}); /* (4) */

These examples all have something in common: the compiler doesn't have quite enough information to infer their concrete types. Instead, it infers what it can about their general shape but leaves some parts as generic. We can observe the following:

  1. In (1), the type is inferred as 'a => ('a, 'a, 'a).
  2. In (2), the type is inferred as 'a => [> `wrap('a)].
  3. In (3), the type is inferred as 'a => {. x: 'a}.
  4. In (4)...