Getting Up and Running With F# in Visual Studio Code on macOS
Setting up Visual Studio Code for F#
- Cool: you can open a Terminal in VSCode with
^ + `
- Do I need to install an extension to use F#? Syntax colouring is built in, looks like
- Let’s try creating an FSX script file
- Ok, I can do that, it has syntax highlighting, but I have no way to, you know, evaluate the scriptfile
- Let’s see how you install F# in the modern .NET Core world…
- I’m pretty sure I already have .NET Core installed via Homebrew
- Yup, got the
dotnet-sdk
cask
- There’s the
ionide-f#
extension, but its website feels outdated, like it talks about needing Mono to get F#, which I’m pretty sure isn’t true. I want to do the most minimal and most up-to-date thing - Ok, this Microsoft doc says “Ionide will generate .NET Framework F# projects, not dotnet core, which can have cross-platform compatibility issues. If you are running on Linux or OSX, a simpler way to get started is to use the command-line tools.”
- That links here: https://docs.microsoft.com/en-us/dotnet/fsharp/get-started/get-started-command-line
- Which talks about creating a solution and project, I just want to run a script
- Good old searching for dev info on things that change rapidly: everything you find is out of date 🙄
- Ah,
dotnet --help
includes “Additional commands from bundled tools”, which includesfsi
, butfsi
is not a command on my path currently. So I have to figure out how to execute it… (I wonder if Homebrew Cask installed it, but only putdotnet
in my path…) - Ohhh… the first post in this thread inadvertently demonstrates that you use it like this:
dotnet fsi
😏 - When I run it, this message appears:
> Failed to install ctrl-c handler - Ctrl-C handling will not be available. Error was: Could not load file or assembly 'Mono.Posix, Version=2.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756'. The system cannot find the file specified.
- Nonetheless, if I write
printfn "Hellow World!";;
, it obediently prints it, and its evaluation thatval it : unit = ()
- From here, we should be able to just provide the filename of an FSX file to FSI, and it’ll run it. Let’s try
> dotnet fsi ~/dev/dotnet-projects/fsharp/test.fsx
- Despite that error message up there, hitting
^C
did indeed terminate the interactive prompt - Now let’s try that command above to run the script
- Yup, it prints Hello World! Success! 🤩
- Ah, from the FSharp tutorial I linked above, you can run commands from the interactive prompt by prefixing them with
#
, e.g.#quit
quits the prompt - Ok, cool: I can write scratch code in an FSX file and then run it at the built-in terminal. This will come in useful as I read the book version of FSharpForFunAndProfit
Intellisense
- It looks to me like this is what you need the Ionide extension for VSCode for. Let’s try installing it and see if it does anything to my FSX file
Ionide-fsharp
- “This extension is recommended based on files you recently opened”, I reckon that’s a good sign.- Its last update was six days ago, which is also a good sign
- Ah, I have a little button in the upper-right of the tab bar that lets me run the script or send to FSI. When I try to run script, it opens the terminal and says “fsharpi: command not found”, so evidently it’s trying to use some runner that I don’t have 🤔
- Not sure how to get it to show me Intellisense errors, either. Might only work on files inside the typical .NET Solution/Project structure?
- Annoying: There are a set of commands added to the VSCode command bar prefixed with
F#:
, but if you typeF#:
into the bar, it ignores the non-alphanumeric characters and finds every fucking command with an F in it, and of course the F#, perhaps because they’re newest, are all at the bottom of the list 🙄 - Intellisense is clearly not working, because I wrote the word “inference” in a comment, declared a value
i
, and when I typedi
elsewhere (to assign the value as one of the values in a tuple), VSCode’s default Intellisense helpfully suggested “inference” instead of the, you know, actual symbol… - Well, let’s try creating a project from the command bar and see what Ionide does with it
- It complains there’s no open folder. True! Let’s open
~/dev/dotnet-projects/fsharp
- I wonder if it can’t evaluate my file because of the missing
fsharpi
- Ooookay: the New Project thingy asked for a folder name and a project name and then said it succeeded, but I don’t see a new folder or project anywhere that I’d expect…
- The F#: Get Help command does sweet fuck all
- Maybe I should restart Visual Studio?
- Nah that didn’t make a difference
- This extension’s lack of feedback is frustrating. It just doesn’t work and doesn’t seem to indicate why
- This troubleshooting section has some things to try
- this suggests I need to install mono…
Time Passes…
Getting F# Projects Working
- Get Started with F# in Visual Studio Code - Microsoft Docs looks like a promising walkthrough
- I already have Ionide installed in VSCode (see above), so let’s see if I can just create a project on the CLI and load it
- Cool, that’s working. I’ve got an Ionide icon in the sidebar now that shows me the solution explorer; looks like Intellisense works!
- Nice: the scripting example works, too. If I save a file as
.fsx
(I’m saving it in the project’s directory, I don’t know if this is required, or if it’ll parse any FSX file)- I just tested it: it parses an FSX on my Desktop just fine 👍🏼
- Sweet: I now have a programming environment I can use to learn more about F# and write some useful programs with it.