Skip to content
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

Decider: decide fn returns Result #51

Merged
merged 1 commit into from
Jan 16, 2025
Merged

Conversation

idugalic
Copy link
Member

Using Result encourages explicit handling of errors, making your code more predictable and robust. Unlike exceptions, which can propagate invisibly, Result types must be explicitly handled using pattern matching or methods like unwrap(). This explicit handling ensures that every possible error is accounted for, which reduces unexpected behaviors and crashes in production.

Decider just got a new generic param Error defaulting to Unit (())

pub struct Decider<'a, C: 'a, S: 'a, E: 'a, Error: 'a = ()> {
    /// The `decide` function is used to decide which events to produce based on the command and the current state.
    pub decide: DecideFunction<'a, C, S, E, Error>,
    /// The `evolve` function is used to evolve the state based on the current state and the event.
    pub evolve: EvolveFunction<'a, S, E>,
    /// The `initial_state` function is used to produce the initial state of the decider.
    pub initial_state: InitialStateFunction<'a, S>,
}

With this change, the decide function now returns a Result, wrapping the vector of events. Events are modeled as enums (following a sum type relationship), allowing for the representation of domain-specific error events. This design choice means that errors can be returned immediately as an Err variant rather than being expressed as events that need to be stored. It gives more options in handling error scenarios!

THIS IS A BREAKING CHANGE - Decider.decide is affected.

@idugalic idugalic merged commit 68af48e into main Jan 16, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant