Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn Type-Driven Development
Learn Type-Driven Development

Learn Type-Driven Development: Benefit from type systems to build reliable and safe applications using ReasonML 3

By Yawar Amin , Kamon Ayeva
$29.99 $20.98
Book Dec 2018 180 pages 1st Edition
eBook
$29.99 $20.98
Print
$38.99
Subscription
$15.99 Monthly
eBook
$29.99 $20.98
Print
$38.99
Subscription
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 26, 2018
Length 180 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781788838016
Category :
Table of content icon View table of contents Preview book icon Preview Book

Learn Type-Driven Development

Starting Type-Driven Development

In this book, we are exploring the techniques and idioms available in type-driven development. Some people also refer to type-driven development as type-level programming. Static types offer several benefits, including:

  • Preventing incorrect code from getting a chance to run
  • Documenting the current codebase
  • Helping to correctly refactor the codebase by pointing out any parts of code you may have missed
  • Offering richer IDE support, for example, auto-completion
  • Better performance when the compiler knows types and can optimize code accordingly

Type-driven development is the practice of using static types to restrict what your code can do. Normally, your programming language gives you enough power to represent any computation. With type-driven development, you are essentially trying to make it impossible for your code to do undesirable things.

In this chapter, we will do some basic critical analysis of a piece of code and look at the possible errors it may contain. We'll also introduce ReasonML, the language we will use to learn type-driven development and compare it with JavaScript. We'll get started with a basic Reason project and then introduce Reason, as well as its related communities and ecosystems.

In this chapter, we will cover the following topics:

  • The main idea and benefits of type-driven development
  • Dynamically typed code versus its statically typed ReasonML equivalent
  • The Reason language, ecosystem, and related projects
  • How to set up a basic Reason project, which we will use throughout this book
  • The Try Reason online playground

Analyzing code for hidden errors

Let's suppose that you have the following JavaScript:

// src/Ch01/Ch01_Demo.js
function makePerson(id, name) { return {id, name}; }

A lot of things can go wrong with the preceding code; they are as follows:

  • The caller can pass in nulls or undefined values as arguments
  • The caller can pass in unintended types of arguments
  • The caller can manipulate the returned person object any way they like, for example, they can add or remove properties

In other words, this code doesn't prevent a number of potential errors. In JavaScript, we have linters, such as ESLint (https://eslint.org/), that check for a lot of possible errors, but you have to remember to find them, enable them, and then work around their limitations. A linter can be helpful in various other ways, such as by pointing out the recommended best practices in a coding style. However, linters in JavaScript are often re-purposed to perform static type checking tasks as well; because they offer so much flexibility and need to be configured (in fact, people usually upload their preferred sets of configuration for different styles of programming), there may be large differences in what exactly gets checked across different codebases.

Adding types

With a static type system, we can restrict our makePerson function in quite a few ways. Here's an example using ReasonML, the language that we're using in this book to learn type-driven development:

/* src/Ch01/Ch01_Demo.re */
type person = {id: int, name: string};
let makePerson(id, name) = {id, name};

Here, we define a new data type, person, and a function that creates a value of the type given the required arguments. We have one more line in the preceding code than we do in the JavaScript code, but in exchange, we get the following guarantees:

  • The caller cannot pass in null or undefined arguments
  • The caller cannot pass in the wrong types of arguments
  • The caller cannot mutate the result value of the function

Notice in the previous example that we didn't have to declare the argument or types for the makePerson function. This is because ReasonML has great type inference that automatically understands that int, string, and person must be the only possible types allowed for those parts of the function.

ReasonML will compile the previous code into the following JavaScript:

// src/Ch01/Ch01_Demo.bs.js
function makePerson(id, name) { return [id, name]; }

As you can see, the preceding code looks almost exactly like the JavaScript we wrote earlier—the main difference is that Reason's JavaScript compiler turns records (which we'll explore later) into JavaScript arrays to take advantage of their speed.

This is just a glimpse of what static types can do to your codebase. In the coming chapters, we'll have a look at many more practical applications.

ReasonML

We're going to explore type-driven development using ReasonML (https://reasonml.github.io/). Reason is a JavaScript-like syntax and is also a set of tools for OCaml (https://ocaml.org/). OCaml is a mature statically typed functional programming language with excellent support for object-oriented and modular programming.

We're going to write Reason code and compile it to JavaScript using the BuckleScript compiler (https://bucklescript.github.io/). BuckleScript takes input from Reason code and outputs essentially a simple subset of ES5 (that is, no ES2015-style classes, no arrow functions, and so on). This will allow us to write strongly statically typed code and see what the output JavaScript looks like with all the types stripped away.

BuckleScript, by default, outputs JavaScript files with the extension .bs.js to distinguish them from your other JS files. You can see this in the example output file, src/Ch01/Ch01_Demo.bs.js.

The Reason toolkit currently consists of:

  • A code formatting and syntax translation tool, refmt
  • An interactive code evaluation environment, rtop
  • A build manager for native-compilation projects (we won't need this one for this book), rebuild
  • A tool that provides intellisense abilities to editors, ocamlmerlin-reason

These tools work together to provide a minimal, yet powerful, development experience. Together with a good editor (we recommend Visual Studio Code), they cover most of your day-to-day development needs.

Why ReasonML?

So why have we chosen ReasonML over something else? For example, TypeScript and Flow are popular languages that target JavaScript today (among many others), but we chose Reason because:

  • It has a powerful and elegant type system, which neatly fits together many type-driven development concepts
  • Its JavaScript compiler (BuckleScript) has incredibly fast compiles, optimization, and high-quality dead-code elimination; fast compiles are great to have if you’re doing type-driven development, and performant code is great to have in any system
  • It has a very helpful and enthusiastic community that's very accessible
  • It gives you access to the mature OCaml community and its aggregated knowledge base

We will take advantage of the contrasts between the two languages to understand how statically typed Reason code is converted into dynamically typed JavaScript code yet still runs safely by design.

Getting started with ReasonML

The Reason website has a great quickstart guide as well as tutorials for setting up editor support. First, install NodeJS to get the node package manager (npm). Then, run the following:

npm install -g bs-platform
cd <your-projects-folder>
bsb –init learning-tydd-reason –theme basic-reason
cd learning-tydd-reason

Now we can do an initial compile with the following command:

bsb -make-world

The preceding command builds your entire project and its dependencies recursively. It will be almost instantaneous.

It's worth mentioning that we actually recommend running the preceding shell commands (substituting in your actual projects folder, of course), because throughout this book, we're going to arrange the code examples in the form of a single project, learning-tydd-reason, and the code examples that you type into the various given file names will fit together to make up that project.

You will almost certainly want to set up editor support in Reason so that you can get things like autocompletion and go to definition. The guides available on the ReasonML website (https://reasonml.github.io/docs/en/global-installation.html) are very helpful for this. Currently, Visual Studio Code (http://code.visualstudio.com/) is the best-supported editor; you will probably get the best results from using that.

If you are trying to decide on the install method, we would personally recommend the OPAM method (OPAM is the abbreviation of OCaml Package Manager).

Using Try Reason

Reason provides a fantastic resource for learners: an online Reason-to-JavaScript compiler and evaluator. To access it, go to the Reason website and click Try in the navigation bar at the top. You can use it to quickly try out different ideas.

Let's run through a quick example using Try Reason to get our bearings. Type in the example code from src/Ch01/Ch01_Demo.re into the Reason section of the Try Reason web app. Now add the following line after that:

let bob = makePerson(1, "Bob");

Now if you examine the output JS, you should see that the following changes have been made:

  • Types have been stripped away
  • Records have been transformed into arrays without field names (records are roughly like C structs or JavaScript objects)
  • Every declared value is explicitly exported (made public)

Note that we have purposely introduced very little actual Reason syntax in this chapter. If you are curious to explore the syntax (which is very similar to JavaScript at its core), it's best if you explore the excellent Reason website documentation. Since the focus of this book is type-driven development, in the upcoming chapters we will introduce all the syntax we will need and discuss its impact on our understanding of the code.

Going further

The ReasonML community is a helpful, fast-growing one. If you need help with anything, don't be afraid to ask. You'll only be a beginner once, and once you're comfortable, you'll be able to help other beginners. Check out the community page at https://reasonml.github.io/docs/en/community.html and drop by the discord chat as the first point of contact.

Summary

In this chapter, we introduced the basic ideas of type-driven development and critically analyzed a piece of dynamically-typed code to explore its potential error conditions that would be prevented by adding static types. We also introduced the ReasonML language and its ecosystem, set up our own Reason project, and got a glimpse of how it can compile statically typed code to JavaScript.

The next chapter will be an important one—we'll delve more into types, values, and working in Reason. See you there!

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • 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

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

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Dec 26, 2018
Length 180 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781788838016
Category :

Table of Contents

12 Chapters
Preface Chevron down icon Chevron up icon
Starting Type-Driven Development Chevron down icon Chevron up icon
Programming with Types and Values Chevron down icon Chevron up icon
Packaging Types and Values Together Chevron down icon Chevron up icon
Grouping Values Together in Types Chevron down icon Chevron up icon
Putting Alternative Values in Types Chevron down icon Chevron up icon
Making Types That Can Slot into Any Other Type Chevron down icon Chevron up icon
Making Types That Represent Operations Chevron down icon Chevron up icon
Reusing Code with Many Different Types Chevron down icon Chevron up icon
Extending Types with New Behavior Chevron down icon Chevron up icon
Bringing It All Together Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.