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

Result should allow errors of any type #2

Open
nozzlegear opened this issue Sep 1, 2018 · 2 comments
Open

Result should allow errors of any type #2

nozzlegear opened this issue Sep 1, 2018 · 2 comments
Labels
enhancement New feature or request

Comments

@nozzlegear
Copy link
Owner

Not all Result use-cases need an actual Error object to represent the error. Sometimes you might just want to return a string. The Result monad should be refactored to accept two generic times: one for the result and one for the error. It should use an underlying union type to determine what is a result and what is an error.

// A function that returns a Result containing either the parsed number, or an error message.
function parseNumber(str: string): Result<number, string> {
    const parsed = parseInt(str):

    if (isNaN(parsed)) {
        return Result.ofError("Parsed string was not a number!");
    }

    return Result.ofValue(parsed);
}
@nozzlegear nozzlegear added the enhancement New feature or request label Sep 1, 2018
@nozzlegear
Copy link
Owner Author

Any Result function that could conceivably throw an error (e.g. Result.ofFunction or Result.ofPromise) should require a callback function that would specifically map the error into something. We want to guarantee that the error value of a Result is what the compiler says it is.

@nozzlegear
Copy link
Owner Author

Result.mapError should also be added to map the error into something else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant