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
€18.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
Paperback 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
€18.99 per month
Full star icon Full star icon Full star icon Full star icon Full star icon 5 (1 Ratings)
Paperback 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 a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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 : 9781788838016
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

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

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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.