I am trying to simulate a side effect of writing to DB with rescript.
So I want to push the data in an array when calling repository.add
. Js.Array.push
returns an int
, which I do not care about. I would like to force to return unit
so that my signature shows unit
which lets me know immediately that this function produces a side effect.
Here is the code (and a playground here):
module Person = {
type entity = {
firstName: string
}
type repository = {
add: entity => unit,
getAll: unit => array<entity>
}
let createRepository = (): repository => {
let storage: array<entity> = []
{
add: entity => {
Js.Array.push(entity, storage) // This has type: int -> Somewhere wanted: unit
() // how to force to return 'unit' there ?
},
getAll: () => storage
}
}
}