Showcase where type is extremely useful in your program?


#1

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.


#2

I use similar pattern a lot here


#3

wow. that’s awesome. thank you


#4

Great example @alexfedoseev !

@thangngoc89 Have you seen this video yet? This example of using ML typing to make invalid states a compile error blew me away! This changes how I think of a simple react state as well.

at 18min:


Question about Facebook's strategy with Reason & Flow
#5

Thank you. Watching it right now


#6

Richard Feldman and Evan Czaplicki are great teachers of making invalid states impossible. Two insightful videos:

Making Impossible States Impossible. https://www.youtube.com/watch?v=IcgmSRJHu_8

The Life of a File. https://www.youtube.com/watch?v=XpDsk374LDE