-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exhaustive AND have a default? #24
Comments
Hello! This is an interesting situation where are your type-time expectations differ from your runtime truth. I think it would be appropriate to add a guard in front of the match, accounting for this unanticipated widening of the possibilities. const calculateIfBattleWasAbandoned = (reason: BattleFinishedReason): boolean => {
if (isOfVariant(reason, BattleFinishedReason)) {
return matchKind(reason, {
timeout: () => true,
surrender: () => true,
attack: () => false,
cancel: () => true,
});
} else {
// Saving future me some headaches
// log the edge case
return false;
}
} If const validTypes = [
`attack`,
`cancel`,
`surrender`,
`timeout`,
]
const calculateIfBattleWasAbandoned = (reason: BattleFinishedReason): boolean => {
if (validTypes.includes(reason.type)) {
return matchKind(reason, {
timeout: () => true,
surrender: () => true,
attack: () => false,
cancel: () => true,
});
} else {
// Saving future me some headaches
// log the edge case
return false;
}
} This gives an error checking flow outside the match allowing us to handle the unexpected cases, while also benefiting from exhaustiveness for when things do work out our way. How does that strike you? |
Yes this is effectively the solution I came ypu with too.. export const calculateIfBattleWasAbandoned = (reason: BattleFinishedReason ): boolean =>
matchKind(reason, {
timeout: () => true,
surrender: () => true,
attack: () => false,
}) ?? false I thought I would bring up up incase there was a better way tho. Thanks :) Feel free to close. |
It's been a while, but I never closed this because I intend to implement this fallback behavior. It makes sense, the default case handling through Match will get a helper function (like partial) and matcher will get another function that achieves the same functionality. |
Hello, I have a new update that should address this issue. Please let me know if I've missed any of the desired functionality. These changes are available in I've added a export const calculateIfBattleWasAbandoned = (reason: BattleFinishedReason): boolean =>
match(reason, withFallback({
timeout: () => true,
surrender: () => true,
attack: () => false,
}, () => false)) For export const calculateIfBattleWasAbandoned = (reason: BattleFinishedReason): boolean =>
matcher(reason)
.when(['timeout', 'surrender'], () => true)
.when('attack', () => false)
.complete({
withFallback: () => false
}) On the redux side, my export const rootReducer = (state = initState, action: Action) => {
return matcher(action)
.when(types(AppAction), _ => ...)
.when(types(GameAction), _ => ...)
.complete({
withFallback: _ => state, // pass through redux init and persist actions
})
}; which I would call an upgrade. I hope you feel the same. |
I am still loving Variant and using it on a daily basis so MASSIVE thanks for this awesome lib :)
Up to now Variant has done just about everything I wanted. I have however started to run into a scenario that is causing problems. Its best described with an example.
e.g. I have the following
This is great, I release this code as v1. The server and client are working great.
I now do a v2 of the game and add a new type to the union..
The compiler will now yell at me that there is a missing option in the
calculateIfBattleWasAbandoned
match which is great, so I add that:I then push the code as a v2 release. The server is happy because it can handle the new case but the problem is there are clients still on v1 of the game and so their
calculateIfBattleWasAbandoned
matcher doesnt know how to handle theCancel
BattleFinishedReason
..I know this can be solved with a
default
option in the matcher:... but this breaks the exhustive checking which I rely on so much.
So is it possible for the matcher to be both exhaustive AND have a default?
The text was updated successfully, but these errors were encountered: