Variant Constructors

In this lesson, we'll learn how constructors allow the variant to use different types.

Constructor Properties

In the previous lesson, we saw a glimpse of a variant’s constructors. A constructor is a special type that uses the basic types available in Reason to create a new value. Its name always starts with a capital letter.

The constructors in a variant are separated by the pipe operator, |. We found a similar use for the | operator in switch expressions.

Just like switch chooses one of its cases, a variant allows us to select one of its constructors.

Declaring Constructors

Let’s get started by creating a car variant which has two constructors, Forward or Reverse.

Get hands-on with 1400+ tech skills courses.