Using F# With Visual Studio Code

I've been pushing my friends to try F# for a long time now and to motivate them a bit more, I decided to create this guide for setting up F# with the new light weight editor from Microsoft, Visual Studio Code. The barrier for coding with Microsoft Technologies has been high because it needed individuals to buy Visual Studio. Recently, Microsoft has released a community edition of Visual Studio that is free and gives most of the crucial features of the IDE; however, it's still a large install so it's nice that the open source community has made F# development possible with Visual Studio Code. This guide is geared towards Windows OS, but the instructions can be applied to Linux or Mac OS.

You can download Visual Studio Code from the link here

To get F# working on your respective machines, you'll need to download the libraries from the F# Software Foundation

After downloading both Visual Studio Code and F#, I recommend getting Forge. Forge is an open source project that allows you to create projects, add files, and add references. This is important because references are maintained in a F# project file titled .fsproj. These are xml files that can be difficult to edit. Forge allows you to use commands that will do those edits for you.

I also recommend getting the following F# related plugins as shown in the image. The three plugins are Ionide-FAKE, Ionide-fsharp, Ionide-Paket:

After installing Forge, create a directory for your project. Then, run the following command in powershell as shown below, which will create project called "demo":

After running this, Forge will prompt you for where your project is located and what type of project you wish to create. For this example, I created a console project:

If you open Visual Studio Code and open the folder containing your project, the folder structure will look like this:

Now that you have a project, let's add a fsx file. A fsx file is a script that file that you can run in the REPL. You can use a Forge command to add the fsx file. Run the following command:

Unfortunately, Forge doesn't output any confirmation if it ran successfully. You can check by opening the .fsproj in your editor and look for the following:

Now that the fsx file has been successfully added, let's write some code and run it using the REPL in Visual Studio Code. Open the fsx file, put in the code in the image. Afterwards, select the line and hit Alt+Enter. This will run your code in the REPL and the output will show up in the window at the bottom.

The next post will show a more complex project using Visual Studio Code and F#.