Basic Datatypes and Operators in Reason

Nik Graf
InstructorNik Graf
Share this video with your friends

Social Share Links

Send Tweet
Published 7 years ago
Updated 6 years ago

In this lesson we explore ReasonML's language basic datatypes like integer, float, boolean, string, char, unit and list. Further you will get familiar with the addition operator, several relational operators as well as structural equality.

Instructor: [00:00] Let's get started with the language basics. We already have seen 1 + 1, and if we try 1.1 + 2.2, it will fail since there's a special operator for adding float values.

[00:13] The operators minus, multiply, divide, and module only work for integers. For float, they do as well, if the operator is followed by a dot. To compare values, we can use several relational operators, or structural equality using two equal signs.

[00:36] If you try to compare values of a different type, for example, an integer and a float, it won't work. We can convert an integer to a float using floatofint.

[00:52] In fact, Reason ships a good amount of such utility functions for converting types. All follow the same pattern with destinationtype_of_sourcetype, and the value as a parameter. For example, pool_of_string.

[01:12] There we are at our next data type, Boolean. It's pretty straight-forward, as it only can be true or false. Boolean operators are not, and, as well as or.

[01:28] Strings are also straight-forward. They are limited using double quotes. Strings can be concatenated using two plus signs, and we can also create multi-line strings.

[01:42] While we also can do one-letter strings, Reason comes with a special data type for a single letter, char, for character. We can create one by using single quotes.

[01:56] Sometimes, we want to denote a value to nothing, basically similar to undefined in JavaScript. Reason has a special value for this purpose. It is described as an opening parenthesis followed by a closing parenthesis.

[02:12] This special value has its own type unit, and it's the only element of that type. Therefore, we can't leverage it to make it type-nullable simply because the type unit is not compatible to any other type. As you can see, this isn't valid, and the compiler froze in error.

[02:34] Unit can be used in many cases. One of them would be for functions with side effects don't return anything. For example, print integer.