Let's look at a few interesting examples of Reason's type inference and how it decides what types need to be filled in later, as illustrated in the following snippet:
/* src/Ch06/Ch06_GenericInference.re */
let triple(x) = (x, x, x); /* (1) */
let wrap(x) = `wrap(x); /* (2) */
let makeObj(x) = {as _; pub x = x}; /* (3) */
let greet(x) = print_endline({j|Hello, $x!|j}); /* (4) */
These examples all have something in common: the compiler doesn't have quite enough information to infer their concrete types. Instead, it infers what it can about their general shape but leaves some parts as generic. We can observe the following:
- In (1), the type is inferred as 'a => ('a, 'a, 'a).
- In (2), the type is inferred as 'a => [> `wrap('a)].
- In (3), the type is inferred as 'a => {. x: 'a}.
- In (4)...