You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
let! binding with type annotation without parentheses yields error FS0010: Unexpected symbol ':' in expression #10697
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)
Reacted by Eugene Auduchinok, Maxime Mangel, Edgar Gonzalez and Melvyn Laïly
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
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
}
Activity
cartermp commentedon Dec 16, 2020
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:
:is the error span:and resolve the identifierlet!, if possible:and resolve the identifier, check it's not an operator(and a)to the left start and right end positions, respectivelycartermp commentedon Dec 16, 2020
Though I suppose it could just be allowed, though I'm curious if this is by design (@dsyme)
ScottHutchinson commentedon Dec 20, 2020
It's interesting that the lex.fsl file defines tokens for a lot of bangs, but not
let!oruse!.ScottHutchinson commentedon Dec 21, 2020
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 touse!is supported/useful, but you can do it withuse. The documentation foruse!is mostly missing at https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/computation-expressionsFor example: