Skip to content

let! binding with type annotation without parentheses yields error FS0010: Unexpected symbol ':' in expression #10697

Closed

Description

@ScottHutchinson

Unlike a normal let binding, the let! binding requires parentheses when it has a type annotation. It would be better if this special syntax were not required. It does not seem to be covered in the language specification. Also documentation for let! could be more easily discoverable (e.g., by linking to it from https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/functions/let-bindings). If the syntax cannot be improved, then perhaps at least the error message could suggest a possible fix and/or a codefix could be added to fix it.

Without the parentheses, it yields a compile-time error:
error FS0010: Unexpected symbol ':' in expression

Repro steps

open System.IO

let stream = File.Open("filename", FileMode.Open)
let bufferSize = 64
// let just stores the result as an asynchronous operation.
let result1 : Async<byte[]> = stream.AsyncRead(bufferSize)
// let! completes the asynchronous operation and returns the data.
let! result2 : byte[]  = stream.AsyncRead(bufferSize)

Changing the last line like this fixes it:
let! (result2 : byte[]) = stream.AsyncRead(bufferSize)

Activity

  1. added this to the Backlog milestone on Dec 16, 2020
  2. cartermp commented on Dec 16, 2020

    @cartermp
    Contributor

    Labeling as an improvement to an error message. It's tricky because it is a parse error, so there is a lot less information to work off of without modifying the parser. But it's probably doable. I imagine a quick fix based on a heuristic could also be built:

    • Check that : is the error span
    • Move left from the : and resolve the identifier
    • Move left from the left identifier and resolve the token as a let!, if possible
    • Move right from the : and resolve the identifier, check it's not an operator
    • Add a ( and a ) to the left start and right end positions, respectively
  3. cartermp commented on Dec 16, 2020

    @cartermp
    Contributor

    Though I suppose it could just be allowed, though I'm curious if this is by design (@dsyme)

  4. ScottHutchinson commented on Dec 20, 2020

    @ScottHutchinson
    ContributorAuthor

    It's interesting that the lex.fsl file defines tokens for a lot of bangs, but not let! or use!.

    rule token args skip = parse
     | ident 
         { Keywords.KeywordOrIdentifierToken args lexbuf (lexeme lexbuf) }
    
     | "do!" 
         { DO_BANG } 
    
     | "yield!" 
         { YIELD_BANG(true)  } 
    
     | "return!" 
         { YIELD_BANG(false) } 
    
     | "match!"
         { MATCH_BANG }
    
     | "and!"
         { AND_BANG(false) }
    
  5. ScottHutchinson commented on Dec 21, 2020

    @ScottHutchinson
    ContributorAuthor

    use! has a similar issue, but adding parentheses does not fix it and yields the error message 'use!' bindings must be of the form 'use! <var> = <expr>'. Not sure if adding a type annotation to use! is supported/useful, but you can do it with use. The documentation for use! is mostly missing at https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressions

    For example:

    let getWebPage (url:string) =
         async { 
            let req = WebRequest.Create url
            use! (resp : WebResponse) = req.AsyncGetResponse() // error
            use stream = resp.GetResponseStream()
            use reader = new StreamReader(stream)
            return! reader.ReadToEndAsync() |> Async.AwaitTask
        }
    
  6. added
    Area-Diagnostics
    on Apr 20, 2022
  7. self-assigned this
    on Apr 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area-DiagnosticsFeature Request

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions