Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
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

Arrow left icon
Profile Icon Amin Profile Icon Soumya Mukherjee Profile Icon Kamon Ayeva
Arrow right icon
€15.99 €23.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
eBook Dec 2018 180 pages 1st Edition
eBook
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Amin Profile Icon Soumya Mukherjee Profile Icon Kamon Ayeva
Arrow right icon
€15.99 €23.99
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
eBook Dec 2018 180 pages 1st Edition
eBook
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€15.99 €23.99
Paperback
€29.99
Subscription
Free Trial
Renews at €18.99p/m

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
Table of content icon View table of contents Preview book icon Preview Book

Learn Type-Driven Development

Programming with Types and Values

In the previous chapter, we looked at type-driven development using ReasonML, but what are types exactly? And how do they interact with other parts of your program? How do they help you on a regular basis, and what does it look like to use a strong static type system with type inference?

In this chapter, we will cover the following topics:

  • Setting up an editor workflow
  • Types and values
  • Immutable values and memory
  • Static versus dynamic typing
  • Type erasure
  • Syntax errors
  • Type errors and inference
  • Unification

Workflow

To get the most from this chapter, we will set up a comfortable edit-compile workflow. We recommend placing two windows side-by-side in your editor. VSCode supports this functionality with the View | Split Editor command. On one side, load a Reason source file; on the other side, load the JavaScript output file (once it is initially compiled). Then in a terminal, run the following command:

bsb -w

The preceding command starts a build in watch mode, which automatically recompiles any parts of a project that are affected whenever you change any source code. In fact, watch mode is smart enough to also remove an outdated JavaScript output file whenever its corresponding Reason source file is deleted. The editor will also auto-reload the compiled JavaScript file whenever you save a Reason source file.

In VSCode, you can also open a terminal session directly below the files...

Types and values

Let's set the stage for the rest of the book with a discussion of types and values. At its core, a type is a set of values. Think of the type bool, which is what Reason calls the normal Boolean type. A bool value can be one of two different things: true or false. We say that these values inhabit (live in) the type. Anything else is an error.

This raises an interesting question: what does it mean to say? Anything else is an error' in this context? In fact, why should we care about types at all?

To answer these questions, let's think about what should happen if we try to do the operation "Bob" / 5. What does it mean to divide the string Bob by the number 5?

If you can't think of a good answer, well neither can anyone else. It's kind of a meaningless question. It's like asking, how does the color green taste? (Although this...

Static and dynamic environments

Let's develop a mental model for what happens in a program with types and values. At its core, a program is made up of a series of type and value definitions. For example:

/* src/Ch02/Ch02_Demo.re */
type person = {id: int, name: string};
type company = {id: int, name: string, employees: list(person)};

let bob = {id: 1, name: "Bob"};
let acmeCo = {id: 1, name: "Acme Co.", employees: [bob]};

Here, we're defining person and company types, and then allocating a person (bob) and a company he works for (acmeCo).

Without worrying too much about the syntax (we will introduce this in Chapter 4, Group Values Together in Types), let's think about how the programming environment sees this program.

In a statically typed programming language, the typechecker and runtime environment together make up the static and dynamic environments...

Errors

We mentioned earlier that the compiler will raise errors if it cannot make sense of a piece of code it comes across. There are a few different kinds of compiler errors, and they are as follows

  • Syntax errors
  • Type errors
  • Name errors
  • Stale interface errors (which we'll cover in the next chapter)
  • Compiler bugs (these are rare but shouldn't be discounted)

The two most common types of error that we will deal with are syntax errors and type errors. Name errors are fairly simple to avoid: always start type names with a lowercase letter and ensure that the names you refer to in your code were defined before you refer to them. (Reason supports cyclic references but not forward references; we'll cover cyclic references later on.)

Syntax errors

...

Summary

In this chapter, we covered a lot of ground, including setting up an editorial workflow, learning about types and values, static and dynamic typing, Reason's separation between compile time and runtime and its type erasure, syntax, and type errors, as well as inference and unification.

In future chapters, we will build on this chapter and introduce many more static typing techniques and the potential type errors that we may see from using them.

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.

Who is this book 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.

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

Product Details

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

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

Product Details

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

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 91.97
Learn Type-Driven Development
€29.99
Modern JavaScript Web Development Cookbook
€36.99
ReasonML Quick Start Guide
€24.99
Total 91.97 Stars icon

Table of Contents

11 Chapters
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

Rating distribution
Full star icon Full star icon Full star icon Full star icon Full star icon 5
(1 Ratings)
5 star 100%
4 star 0%
3 star 0%
2 star 0%
1 star 0%
Johann Hubert Sonntagbauer Apr 27, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I always failed to give my data layer the right amount of semantics. This book introduced me in a whole new world of possibilities thanks to the mighty ReasonML/OCaml type system.Decorating arbitrary Strings with semantics and behavior blew my mind, not even mentioning the advanced techniques described in the book. Enjoyed every chapter and loved the examples.Some advanced patterns need to sink in before I could appreciate the value, so I definitely need to revisit some chapters again.
Amazon Verified review Amazon
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.