effect
effect copied to clipboard
Enhance sum type workflow (Data.taggedEnum)
What is the problem this feature would solve?
Working with sum types (tagged unions) must be cheap to easily create models which prevents storing impossible states inside them.
I suggest adding new helpers for Data.taggedEnum:
$matchnon-exhaustive support$get$map
What is the feature you are proposing to solve the problem?
import { Data } from "effect"
type RemoteData = Data.TaggedEnum<{
Loading: {}
Success: { readonly data: string }
Failure: { readonly reason: string }
}>
const { $is, $match, $get, $map, Loading, Success, Failure } =
Data.taggedEnum<RemoteData>()
// Non exhaustive matcher: (RemoteData) -> Option<_>
const matcher = $match({
Success: ({ data }) => `this is a Success: ${data}`,
Failure: ({ reason }) => `this is a Failure: ${reason}`
})
// Getter: (tag) => (state) => Option<Variant>
const maybeSuccess = $get('Success')(Loading())
// Map: (tag) => ((singleState) => newState) => (state) => computedState
const singleMapper = $map('Success')(({ data }) => ({ foo: data }))
// Map: ({Variant: (singleState) => newState}) => (state) => computedState
const multiMapper = $map({
Success: () => { foo: 'bar' },
Loading: () => { other: 42 },
// Missing Failure leaves it the same
})
What alternatives have you considered?
Manual implementation.
Willing to send a PR in case of you like my suggestions.