OCaml data types and variables

It doesn't take too much to familiarize yourself with the concepts behind variables and the OCaml type system, even if you come from languages with a different approach.

Let us analyze the main features and differences when it comes to working with variables

1. Values are bound to an identifier by using let. This is called let-binding and is the similar to the way other languages declare variables
(* 10 is bound to myVar *)
let myVar = 10;;

These so called variables are immutable. Once the values are assigned, they cannot be changed later. Let-binding is used for many other things, like functions, objects and any kind of expression

2. Variable names must start with lowercase letter or underscore

Otherwise, the compiler will output an error: Unbound contructor UpperCaseVar, treating UpperCaseVar not as a variable but as a constructor.

3. The type of the variable is automatically deducted (type inference)

The OCaml system will understand by itself that the variable above is an integer

4. However, you will need to use different operators for integers and floats
(* Integer + Integer *)
let myIntVar = 10 + 10;;
(* Float x Float *)
let myFloatVar1 = 5.0 +. 2.4;;
(* Float x Float *)
let myFloatVar2 = myFloatVar1 *. 1.2;

This way we will help the compiler know that myFloatVar1 and myFloatVar2 are floats. Operations between different types will throw an error. OCaml is very good at this and will help you detect many bugs before running the code

5. Explicit casting is available with the help of several functions (float_of_int or float, int_of_float, char_of_int, int_of_char, string_of_int)
(* The integer is casted to float *)
let myCastedGloat = float 10 +. 1.2
6. Variables are visible within their scope, like in many other languages

Variables from outer scopes are shadowed by those within an inferior scope, when having the same name

(* Outers scope *)
let myVar = 10;;

(* Expression with its own scope *)
let doubleVar =
    (* Inner scope. Here, the value will be 20, not 10 *)
    let myVar = 20;;

(* Back to outer scope. Value is 10 *)
7. Basic accepted data types: integers, floating point numbers, booleans, characters, and strings
(* Boolean *)
let myBoolVar = false;;
(* Character *)
let myCharVar = 'a';;
(* String *)
let myStringVar = "Hello there";;
8. An integer value will use the last bit to take care of garbage collection

Therefore, integers only have 31 bits or 63 bits on 64-bit platforms, unlike other languages. Also, for unsigned int you could use the nativeInt module.

9. Unicode characters are not supported by default

But there are libraries for this, like Camomile

10. Advanced structures that acts as data types: arrays, lists, tuples and others, usually provided by modules
(* Arrays have fixed length and the same type for each item *)
let myArrayVar = [| 1; 2; 3; 4 |];;

(* List have variable length and the same type for each item *)
let myListVar = [ 5; 6; 7; ];;

(* 3-Tuple with integer, string and boolean. Tuples have fixed length but different types *)
let my3TupleVar(num, text, flag) = (42, "hello", true);;
11. The unit type is used as void in other languages

It has no value and is returned by built-in printing functions or foor loop and while expressions

12. References are used to create actual mutable variables, with values that can be changed
(* Create a pointer to the 0 value *)
let myVarPointer = ref 0;;

(* Change the value *)
myVarPointer := 10;;

(* Return the content of the reference *)

This is almost everything you need to know in order to understand the nutshell of data types and basic operations with variables. OCaml will let you create your own types and structures, but we will dedicate another article to that


You can test all the code displayed here by using the OCaml interactive console, which is installed with the entire system. Open it by executing ocaml. Type the code and press ENTER to see the result.

Remember that definitions and expressions must end with ";;"


Specify your e-mail if you want to receive notifications about new comments and replies