Hi. It is just a post for discussion. I know there is no silver bullet for all use cases, but when coming from TypeScript to ReScript type system, you need to change your view drastically.
For example, I am modelling simplified terminal window, and want to expose methods to manipulate output in two cases - add/remove lines and scroll the viewport (when there are more lines than can be shown). What is the recommended way to deal with API:
a) Provide a single type of all use cases and single method - run
b) Provide two types for each part and two methods - screen, viewport
In an essence, I think this is a question about: 1) should I nest my data types or not, 2) should I transfer responsibility from method to data type, 3) how distinct or similar method and data type when dealing with business logic modelling.
Maybe there are some books, articles and examples that you can recommended to get a better grasp on this!
Code for single method:
type direction = Up | Down | Reset
type command =
| Clear
| Echo(string)
| Focus(direction)
type t = {
lines: array<string>,
run: command => unit,
}
let useDisplay = () => {
let lines = []
let run = command => {
switch command {
| Clear => ()
| Echo(line) => ()
| Focus(direction) => ()
}
}
{lines, run}
}
Code for two methods:
type direction = Up | Down | Reset
type command =
| Clear
| Echo(string)
type t = {
lines: array<string>,
screen: command => unit,
viewport: direction => unit,
}
let useDisplay = () => {
let lines = []
let viewport = direction => {
switch direction {
| Reset => ()
| Down => ()
| Up => ()
}
}
let screen = command => {
switch command {
| Clear => ()
| Echo(line) => ()
}
}
{lines, screen, viewport}
}