【发布时间】:2022-02-07 00:32:56
【问题描述】:
我正在尝试模拟使用 rescript 写入 DB 的副作用。
所以我想在调用repository.add 时将数据推送到数组中。 Js.Array.push 返回一个 int,我不关心。我想强制返回unit,以便我的签名显示unit,这让我立即知道这个函数会产生副作用。
这里是代码(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
}
}
}
【问题讨论】:
标签: rescript