The Unit Data Type

This lesson will highlight the features and uses of the unit data type.

What is Unit?

Until now, we’ve seen that every data type creates an instance containing some value. An int has numbers, whereas a string has characters.

However, there is a special type in Reason used to represent an empty value. This is the unit data type. As the name suggests, it is just a single unit without any value. The unit type is represented by parentheses, ().

The Purpose of Using Unit

So, where would the unit type be useful? The answer lies in functions. There exist functions in which we do not need to pass any arguments. Such a function could simply be printing something to the console or making computations not dependent on specific arguments.

In such a case, the parameter type of the function is unit.

Example

Let’s take a look at the simplePrint() function which prints “Hello World”:

Get hands-on with 1400+ tech skills courses.