From the course: ReasonML: First Look

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Creating your own data types

Creating your own data types - ReasonML Tutorial

From the course: ReasonML: First Look

Start my 1-month free trial

Creating your own data types

- [Instructor] Remember this code that we developer earlier to find the price of a cake given its size? Everything works great if we have a valid string like half, but an invalid string like small gives us a price of zero. Let's run this program to show that. We can prevent this sort of error, giving invalid strings, by creating a datatype for cake sizes that allows only valid choices. In this file, caketype.re, we're going to define a type named cakeSize. Type names must begin with lowercase. And then preceded by vertical bars, we give the values that tell how to construct a cake size type. It can be either full or half or quarter. These values must begin with an uppercase letter. Now that we have a constructor for the datatype, we combined values of that type to variables. For example I can let cake1 equal half. If I try to use an invalid value such as small I get an error right away that says unbound constructor.…

Contents