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

Move to the try Keyword for Error Propagation #7087

Closed
Tracked by #7086
smores56 opened this issue Sep 18, 2024 · 0 comments · Fixed by #7193
Closed
Tracked by #7086

Move to the try Keyword for Error Propagation #7087

smores56 opened this issue Sep 18, 2024 · 0 comments · Fixed by #7193

Comments

@smores56
Copy link
Collaborator

smores56 commented Sep 18, 2024

We are currently using the ? suffix operator to desugar to Result.try, which allows users to propagate errors early. We want to move to supporting the try prefix keyword, which can be used in values and in pipelines. Example usage looks like the following:

fileContent = try File.read! "file.txt"

jsonData =
    try File.read! "file.txt"
    |> try Json.decode 

try usages early return errors to the nearest function boundary, by basically desugaring to

when result is
    Err err -> return Err err
    Ok val ->
        # rest of the body using val

We need #7104 to be implemented first to implement try.

We should not treat try as a simple desugaring to Result.try, as it needs to eventually work with pure and effectful code. That's why we should desugar to a when statement instead.

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 a pull request may close this issue.

1 participant