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

Use ? for Mapping Result Errors #7088

Open
Tracked by #7086
smores56 opened this issue Sep 18, 2024 · 0 comments
Open
Tracked by #7086

Use ? for Mapping Result Errors #7088

smores56 opened this issue Sep 18, 2024 · 0 comments

Comments

@smores56
Copy link
Sponsor Collaborator

smores56 commented Sep 18, 2024

To promote Roc developers providing context for Results when propagating errors with try, we want to add a new operator ? that acts as a Result.mapErr equivalent. Some example usages:

fileContents =
    try File.read! "file.txt" ? FailedToReadFile

fileContents =
    try File.read! "file.txt"
        ? FailedToReadFile

{} = File.save! "file.txt" contents ? \err =>
    Logging.error! "Failed to save file.txt: $(Inspect.toStr err)"

    FailedToSaveFile err

It should be higher precedence than try such that try will propagate errors after ? is applied, but it does not need to be used with try.

We should always attempt to format onto the same line as the fallible code, but can optionally wrap to a newline with an indentation to imply continuation; this prevents code from reading like we apply ? after try.

The new ? operator should be able to handle mapping over errors using pure or effectful mappers, meaning it should be its own AST node. We can start with a pure-only implementation, since we are waiting on purity inference still.

If a pure handler is used with the new ?? "Result.withDefault" operator, we should raise a warning that the error mapping is unnecessary, since it will always be discarded by ??.

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

No branches or pull requests

1 participant