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

Improve Error Reporting: "inner" expressions inside computations expressions #2739

Open
Tracked by #1103
isaacabraham opened this issue Mar 30, 2017 · 0 comments
Open
Tracked by #1103
Labels
Area-Diagnostics mistakes and possible improvements to diagnostics Feature Improvement Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.
Milestone

Comments

@isaacabraham
Copy link
Contributor

@dsyme - as per your email w/ Dave Thomas...

What

If you create a computation expression that has a nested expression inside it bound to a value, you need to explicitly create a "child" computation expression for the nested expression.

let x =
    let text = "foo"
    async {
        let answer =
            match text with
            | "foo" ->
                let! y = y   // error FS0750: This construct may only be used within computation expressions
                y + 5
            | _ -> 0
        return answer + 10
    }

You can fix it either by explicitly wrapping the nested match expression branches in their own CEs: -

let x =
    let text = "foo"
    async {
        let! answer =
            match text with
            | "foo" ->
                async {
                    let! y = y
                    return y + 5 }
            | _ -> async { return 0 }
        return answer + 10
    }

or the entire child expression

let x =
    let text = "foo"
    async {
        let! answer = async {
            match text with
            | "foo" ->
                let! y = y
                return y + 5
            | _ -> return 0 }
        return answer + 10
    }

Why

The error message is misleading: as far are the developer is concerned, they are in an async CE; it's not clear at all that by creating a nested expression within the parent CE, the user has "left" the CE.

How

If it's possible for the compiler to detect this, the error message should be clearer e.g.

Code within a nested expressions (e.g. "if" or "match") exists outside of any parent computation expression. In order to perform actions such as let! or do!, the nested expression must be wrapped within its own computation expression.

It would also be useful (and this is something that we should consider doing with many other error messages) to give a "before / after" example (as I've done here) either directly inside the compiler error message, or somehow linking to one.

Alternatively, depending on the complexity and whether this could be determined up front, the compiler could do this "nested wrapping" for us.

@cartermp cartermp added this to the F# 4.2 milestone Mar 31, 2017
@cartermp cartermp removed this from the F# 4.2 milestone Jun 9, 2018
@cartermp cartermp added this to the Unknown milestone Aug 25, 2018
@dsyme dsyme added the Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language. label Sep 16, 2021
@dsyme dsyme added Area-Diagnostics mistakes and possible improvements to diagnostics and removed Area-Compiler labels Mar 31, 2022
@vzarytovskii vzarytovskii moved this to Not Planned in F# Compiler and Tooling Jun 17, 2022
@kerams kerams marked this as a duplicate and then as not a duplicate of #3252 Jun 28, 2023
@vzarytovskii vzarytovskii reopened this Jan 4, 2024
@github-project-automation github-project-automation bot moved this from Done to In Progress in F# Compiler and Tooling Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Diagnostics mistakes and possible improvements to diagnostics Feature Improvement Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.
Projects
Archived in project
Development

No branches or pull requests

5 participants