Lesson 6: Working with newtypes

  • 20 minutes
product type Either newtype

In this lesson we want to extend our code to validate usernames as well as passwords; we’d also like to be able to distinguish at the type level between strings that represent inputs (password, username) and strings that represent errors. To do this we introduce newtypes.

We’d also like to keep a username and a password together as a User value, so we write a product type called User. We refactor the code we already have, write new functions that validate username inputs, and write a function that constructs a User from the conjunction of a valid Username and a valid Password.

This lesson will be released after Julie recovers from bronchitis.