2
votes

I am trying to follow the docs to create an f# program in Visual studio Code

When I highlight my code and press Alt Enter to run in the interactive window I get an error

Script.fsx(8,5):error FS0010: Unexpected keyword in binding. Expected incomplete structured construct at or before this point or other token.

enter image description here

[Update]

I tried several times to get this to work, highlighting the code as the image shows. Strangely after leaving the computer an hour, I now can no longer repeat the problem.

1
There's a known bug in Visual Studio Code and the F# Interactive plugin where sometimes, not every time, the code that you select with Alt-Enter will have parts of it missing when it's pasted to the interactive terminal. The bug seems to occur more often with larger chunks of code, so if you're pasting your whole program to the terminal at once, you should try pasting smaller chunks (starting from the top, and working your way down). That may be why you can't reproduce the bug now, because it's intermittent. The workaround is to just paste your code again, and it usually works the second time. - rmunn
The known bug I was thinking of is github.com/ionide/ionide-vscode-fsharp/issues/340. It was closed a few months ago because the developers of Ionide (the F# plugin for VS Code) believed the bug has been fixed. But if you run into this issue (where you send a chunk of code with Alt+Enter but some chunk of your code is missing in the F# Interactive window) again, then adding a note to that bug report is probably the best thing to do. That way the Ionide developers will know that it's still happening, and can reopen the bug report and investigate. - rmunn
Thanks, I linked this question into the issue. - Kirsten
I am experiencing the same issue. Just posted on GitHub too. - stevethethread

1 Answers

3
votes

The output visible in the interactive window suggests that your selection when you pressed Alt+Enter was different from the selection you're showing in the screenshot. More specifically, the selection you executed started with let isVowel (without leading spaces) and ended with word.[0] then.

Even more specifically, the code you tried to execute was this:

let isVowel (c: char) =
        match c with
        | 'a' | 'e' | 'i' |'o' |'u'
        | 'A' | 'E' | 'I' | 'O' | 'U' -> true
        |_ -> false

    if isVowel word.[0] then

This code does not compile for several reasons. First, there is nothing after then. Second, the if is indented incorrectly: it needs to be indented either to the same position as match (in which case it will be considered part of isVowel definition) or to the same position as let isVowel (in which case it will be considered part of the same block as isVowel), but here it is neither - to the right of let isVowel, but to the left of match.

If you wanted to execute just the definition of isVowel, then you shouldn't have included the if in the selection.
If you wanted to execute the whole definition of toPigLatin, then you should have included the let toPigLatin line and the whole if/else expression.