DEV Community

Discussion on: Building Coronate: an open source chess tournament manager

Collapse
 
johnridesabike profile image
John Jackson

Type definitions, like type t = | A | B;, will have to be in both files. Type annotations are only necessary in .rei files. For example:

/* .re file */
type t = | A | B; /* type definition */
let a = A; /* type is inferred */
let b = B; /* type is inferred */
let toString = /* type is inferred */
  fun
  | A => "A"
  | B => "B";

/* .rei file */
type t = | A | B; /* type definition */
let a: t; /* type annotation */
let b: t; /* type annotation */
let toString: t => string; /* type annotation */

Whether you want to also annotate the types of the values in your .re file too is a matter of preference. Sometimes they can help you understand and debug code, but the compiler rarely needs them.

Very often, you won't need to copy the whole type definition to the .rei file, and you can keep it opaque instead. This is handy when your data is complicated and you don't want to leak its internal complexities across your whole project.

Collapse
 
vasco3 profile image
JC

thanks John, good to know. This was very helpful