I have been a heavy Flow user before I come to ReasonML. In that time, I treated the type system as a type checker, whether I’m passing the right type of argument.
I have been treating ReasonML’s type system like that too, until today, I solved a problem pretty easily by leveraging the full potential of type system. Here is my use case:
CodeMirror’s coordinates system uses 0-origin for both line and column
OCaml’s coordinates system on the other hands, uses 0-origin for line and 1-origin for column.
In previous version, I only have one type
type loc = {
line: int,
col: int,
}
Then I have to remember converting line correctly when moving between compiler <-> editor coords
Whenever I touch that part of the program, I feel like opening a jar full of worms because I always forget to add or subtract lines.
Today, I decided to try a different approach, I have 2 types:
type compilerLoc = {
cpl_line: int,
cpl_col: int,
};
type editorLoc = {
edi_line: int,
edi_col: int,
};
Then I have 2 functions: compilerToEditorLoc
and editorToCompilerLoc
. From now on, the type checkers will ensure I converted the location correctly.
It works on first try without a bug (so far).
I feel so powerful with ReasonML right now.
If you have usecase for leveraging to prevent bug like this, please share. I would love to learn more.