This is subjective but I’m just curious if anyone has an opinion on it
I’ve been looking into a lot of “imperative shell, functional core” articles, trying to increase my unit test coverage. Working in vanilla and node I need to come up with my own “shell” to handle mutability and side effect stuff.
Online, the examples I see come from F# and scala where they typically just use classes. Since rescript doesn’t have any idea of classes, I’ve gotta choose between using a single ref or defining mutable fields on a type
Most of the time I reach for a ref
, but eventually I need to start splitting update actions into different functions, and this is where my question lies. Do you prefer to create a type with mutable
, or is passing a ref
around to other functions considered okay?
type state = {
userId: string,
posts: array<post>
}
let fetchPosts = async (state: ref<state>) => {
let posts = await fetch(state.contents.userId)
state := {
...state.contents,
posts: Array.concat(state.contents.posts, posts)
}
}
type state = {
userId: string,
mutable posts: array<post>
}
let fetchPosts = async (state: state) => {
let posts = await fetch(state.contents.userId)
state.posts = Array.concat(state.posts, posts)
}
mutable
seems more idiomatic JS but then we’re back to having to worry if a function will change your state or not. ref
protects you against that but now the signature requires you to ref()
it first (Is that even an issue?)
So what’s your opinion?