Home/Tutorials/ReasonML/Reason Type
ReasonML4 min read

Reason Type

Reason is a syntax on top of OCAML that was geared towards bringing JavaScript Developers closer to the OCAML tool chain and semantics.

Basic Data Types :-


A type is simply an attribute that tells us the kind of data a value can have.

A few examples of basic data types are int, string, Boolean and float

We will start with types because as the reason ML docs say

Every piece of Reason Code has a type

(https://reasonml.github.io/docs/en/type)

Using types to annotate our code



```reason

let age = 12;

```

In the code above we are simply binding the value 12 to age and we have explicitly stated that age is an int

Types can also be inferred.



```reason

let age = 12;

```

This means that reason can know the type of age binding based solely on the value provided to be bound.

Side Note

Let bindings cannot change. If you want to update age from 12 to 13 you have to create a new let binding, this is called immutability.



```reason
let age = 12;
let newAgeAfterBirthday = age + 1;
```

More on immutability later.

Function type annotations.

Functions can also be annotated for example if we create a function for computing the new Age of a person after a Birthday we can write

	

```reason

let newAge = (age: int): int => age + 1;

```

So this function takes an int as an argument and returns the int+1. (Well that’s what happens after a birthday. The current age is incremented by 1.)

The function can still be written without the types and will still be inferred.

	
```reason

let newAge = (age) => age + 1;

```

Type Aliases

Types can take on different names for instance



```reason

type personage = int;

```

With this our types can make our code a but cleaner and our types give a clearer description of the data we are dealing with.

	
```reason

let resolveAgeAfterBirthDay = (x: personage): personage => x + 1;

```

In the code above, we know that x is an int even without the explicit types that are present, but the code is more clearer as we are now aware that the int represents a person’s age.

Containers and their types

Reason Ml has lists and array data types built in. They have different methods but they each contain items of the same type

for instance;

	
```reason

list(int)
list(string)
list(int)
list(bool)

array(int)
array(string)
array(int)
array(bool)

```

They can also take type aliases.

	
```reason
type word = string

let sentence:list(word) = ["How", " Are", " You"]
```

Lists and arrays are just a collection of data of the same type.

We will talk more about them later.

We have just covered the very basics of types in Reason. Take a breather and we’ll cover type arguments next.

Type Arguments

Types can take Arguments. Before I explain how they do this, lets start with an example.

A rectangle can be represented as

	
```reason
type rectangle = (int, int);

```

The structure above is called a Tuple. (An ordered set of values separated by commas)

One int for the length and the other for the width.

We can then create a rectangle as follows.



```reason

let rect1: rectangle = (12, 42)

```

All things are good, up until our manager comes by our office and says that our rectangles need to be more accurate which means that they also need to be represented as floats, where possible.

That’s fair enough, what we can do is simply create another type with floats being the arguments.


```reason

/* type rectangle = (int, int); */

type intrectangle = (int, int);
type floatrectangle = (float, float);

```

So that we can then have


```reason
let rect1:intrectangle = (12, 42);

let rect2:floatrectangle = (12.43, 42.54);

```

This should work, but now we have a lot of duplicate types. They all have the same structure, but the only thing that is different is the types.

We can solve this by creating a type with arguments. What this does is assume that the type is a function and once given a type as an argument returns a new type of argument provided.


```reason

type rectangle('a) = ('a, 'a);

```

So rectangle takes a type a and returns a tuple of the type a that has been given



```reason

type intrectangle = rectangle(int)
type floatrectangle = rectangle(float)

let rect1:intrectangle = (12, 42);
let rect2:floatrectangle = (12.43, 42.54);

```
Or

```reason

let rect1:rectangle(int) = (12, 42);
let rect2:rectangle(float) = (12.43, 42.54);

```

I hope you got a good grasp on types, they can be problematic especially to developers that come from a dynamically typed language.


INTERACTIVE SIMULATION CHALLENGE+250 XP
Workbench Verification

Ready to test "Reason Type" in production?

See how ReasonML algorithms and server loops perform under 100k req/s traffic spikes.

Simulator: SysDesign Ops Simulator