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

feat: allow ResultTryQuestion suffix in AST #6844

Conversation

sekerez
Copy link
Contributor

@sekerez sekerez commented Jun 27, 2024

Makes progress toward #6828, based on this excellent guide!

No tests required changes, I"ll add new automated tests in the desugar followup unless there are recommendations.

@sekerez sekerez requested a review from evanrelf June 27, 2024 23:39
@lukewilliamboswell
Copy link
Collaborator

lukewilliamboswell commented Jun 28, 2024

What do you think about adding a couple of snapshot tests too?

What you have so far looks good to me.

@sekerez
Copy link
Contributor Author

sekerez commented Jun 28, 2024

What do you think about adding a couple of snapshot tests too?

Great point, working on it rn.

@sekerez
Copy link
Contributor Author

sekerez commented Jun 28, 2024

@lukewilliamboswell added snapshot testing by adding analogous examples in files that previously contained only the task bang suffix, lmk if you"d rather I split out into different files

@smores56
Copy link
Collaborator

@sekerez I"m not Luke Boswell, but I"d vote for splitting out into different files. It makes the testing more single responsibility, and we may have the ? and ! behave differently in the future, though I don"t find it likely.

However, I"d understand wanting to keep them in the same file, in that we want any tests updated for ! to also get updated for ?, as their behavior is currently expected to be basically identical.

@kdziamura
Copy link
Collaborator

Just a heads-up: I"m changing the parsing logic a little bit here: #6851
In particular, EmptyDefsFinal will be dropped

@kdziamura
Copy link
Collaborator

kdziamura commented Jul 2, 2024

their behavior is currently expected to be basically identical

What about ResultTryQuestion as a statement? Should it be forbidden or implement early return?

I mean:

and = \resultA, resultB -> 
    resultA? # return resultA if it"s Result.err
    resultB? # otherwise return resultB

@sekerez
Copy link
Contributor Author

sekerez commented Jul 2, 2024

What about ResultTryQuestion as a statement? Should it be forbidden or implement early return?

@kdziamura great point - I feel like it should be forbidden, as if resultB is of type Result T, then returning resultB? would return Err T if it"s an error, and T otherwise... whereas it should instead return Ok T in that case, hence the ? should be omitted from the return.

Just to check my understanding, returning a value with ? should result in a type error, right? And that"s downstream from AST generation? Hence the AST should still generate, right?

edit: I"m a little surprised that code like this compiles:

main =
    Stdout.line! "Hello, World!" # returns {} | Task.Err [...] 

and behaves identically to

main =
    Stdout.line "Hello, World!" # returns Task.ok | Task.Err [...] 

given that in my mind the return type differs, based on the same reasoning for ?.

Is my understanding incorrect? Should we as a matter of convention allow returning resultB?, and wrap it in an Ok if it"s not Err?

@sekerez
Copy link
Contributor Author

sekerez commented Jul 2, 2024

@smores56 that makes sense - I"ll split out into separate files, and changes the original files" name to taskbang from suffixed.

@kdziamura also rebasing on your changes.

@kdziamura
Copy link
Collaborator

kdziamura commented Jul 2, 2024

and = \resultA, resultB ->
    resultA? # return resultA if it"s Result.err
    resultB? # otherwise return resultB

It will be desugared into

and = \resultA, resultB ->
    Result.try resultA \{} ->
        resultB

Which makes sense for consistency (! and ? are interchangeable, it’s only one for Tasks and the other for Results).

So, we can indeed fully piggyback the TaskAwaitBang implementation

@sekerez sekerez marked this pull request as draft July 2, 2024 16:58
@sekerez sekerez marked this pull request as ready for review July 2, 2024 17:41
@@ -550,7 +553,7 @@ pub fn is_expr_suffixed(expr: &Expr) -> bool {
// expression without arguments, `read!`
Expr::Var { .. } => false,

Expr::TaskAwaitBang(..) => true,
Expr::TaskAwaitBang(..) | Expr::ResultTryQuestion(..) => true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let"s also update is_top_level_suffixed function in this file. The behaviour should be the same as for TaskAwaitBang

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After that, some of snapshots will change. In particular, it will affect the postfix in the statement position

Comment on lines +3 to +4
x = B.b? "Foo"
c? x
Copy link
Collaborator

@kdziamura kdziamura Jul 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These statements should be parsed as part of the main
#6844 (comment)

Copy link

github-actions bot commented Aug 2, 2024

Thank you for your contribution! Sometimes PRs end up staying open for a long time without activity, which can make the list of open PRs get long and time-consuming to review. To keep things manageable for reviewers, this bot automatically closes PRs that haven’t had activity in 60 days. This PR hasn’t had activity in 30 days, so it will be automatically closed if there is no more activity in the next 30 days. Keep in mind that PRs marked Closed are not deleted, so no matter what, the PR will still be right here in the repo. You can always access it and reopen it anytime you like!

@smores56
Copy link
Collaborator

@sekerez I realize that I have some spare development capacity, and I see that this PR hasn"t had progress in about a month and a half. Would you mind if I chipped in on getting this out the door?

@smores56
Copy link
Collaborator

I was able to implement this by hijacking the ! -> Task.await code: #7000

@smores56 smores56 closed this Aug 16, 2024
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.

6 participants