Setting Up Visual Studio for F# Development

Setting Up Visual Studio for F# Development

Today I'm going to talk about how to get your environment setup to use F#. As I mentioned before in my last video, I am a C# developer in my regular job. My assumption is that if you are also a C# developer that you use Visual Studio to write the majority of your code and you already have it installed. If you don't use Visual Studio, that is ok, because in my next video I am going to show you how to setup a different editor for writing F#. Today, I am going to walk you through the steps that you need to take in order to start using Visual Studio for F# Development.

Visual Studio

The good news is that if you already have Visual Studio 2013 or Visual Studio 2015 installed, you already have everything that you need to start writing F# code. However, there are a few additional plugins that I recommend in order to have a little bit better experience. Go ahead and open Visual Studio.

  1. Go to Tools > Extensions and Updates

  2. Visual F# Power Tools

  3. Visual F# 4.0

  4. Visual F# Tools Templates

Make sure that you have all 3 of these extensions installed.

Your First F# Project

  1. Go to File > New > Project

  2. Choose Templates > Visual F# > Console Application

  3. Name the Project FSharpDemo

  4. Click "Ok"

When you look at your new project, it should look pretty familiar to C# developers. You have a Solution File, a Project File, and a Program.fs file:

Instead of Program.cs like you are used to, it is Program.fs where fs stands for FSharp instead of CSharp.

Open the contents of the Program.fs file and it should also look familiar to you.

You have a main method that is decorated with an [<EntryPoint>] attribute which is new and different. You can also see that the main method accepts a parameter called argv, which is the arguments that are passed into the console application. On the next line you have a printfn statement with some additional information passed in.

Don't worry about the code or the syntax right now. We will get into that later. For right now, all you need to know is that when you run this program, all it is going to do is print out whatever arguments you pass into it.

You can run the program just like you would any C# console application. You can build the application by pressing Ctrl + Shift + B or by going to Build > Build Solution. You can also run the program by pressing F5 or by going to Debug > Start Debugging.

Add a break point to line 7 and press F5. Once it hits the break point, hover over the argv variable and you will see that it is an empty array of strings.

When you look at the output of the console window you will see that it printed out [||] which is the F# syntax for an empty array.

The reason that this is empty is because we did not pass any arguments to our console application.

  1. Right click on the FSharpDemo project in Solution Explorer
  2. Choose Properties
  3. Go to Debug
  4. In the Start Options > Command line arguments type "Hello World"

Run the program again by pressing F5 and you will see that it outputs an array with 2 strings to the console:
1. Hello
2. World

Now you have your basic "Hello World" F# console application.

F# Interactive

What I want to introduce you to next is something that you probably aren't used to if you are a C# Developer. F# comes with a REPL called F# Interactive. You have worked with other languages such as Python or Node.JS you are probably familiar with what a REPL is. REPL stands for Read Eval Print Loop. REPLs are a nice programming language feature that allows developers to play around with code and quickly create prototypes. A common F# paradigm is to create a script file and use that to create the initial concepts for an application before building out the actual application.

Right click on the FSharpDemo project Add > New Item > Code > Script File. Notice that the file extension is .fsx instead of .fs. This is what differentiates an F# Script file from an F# Source file.

Declare 2 variables a and b and set the values to 5 and 7 respectively. The print the sum of the 2 numbers.

F#

let a = 5  
let b = 7  
printfn "%i" (a + b)  

C#

var a = 5;  
var b = 7;  
Console.WriteLine("{0}", a + b);  

There are a couple of differences between this code and C# code.
1. To declare a variable in F# you use the let keyword vs the var keyword in C#.
2. C# requires a ; to terminate every line.
3. In F# printfn is the preferred way of printing the console although you can use the Console.WriteLine function as well.

Generally speaking, the syntax between the 2 languages are very similar. If you squint your eyes a lot they almost look the same.

Now, select all of the code with Ctrl + A. And press Alt + Enter to run the code in F# Interactive. In the F# Interactive window you will see that it printed out 12 and that you have an integer a = 5 and an integer b = 7.

You can also type directly in the F# Interactive window. When doing this, you must end every command with ;;. So go into the Interactive window and type a;; and it will print out the following information.

val it : int = 5

You can also type a + b;; and it will print out

val it : int = 12

It took me a while to get used to using the F# Interactive window but now that I have been using it for a while I really like it a lot. It makes it very easy to prototype and also to debug code issues. Also, in the future, I will show you how I use the interactive window and script files a lot to complete exercises that helped me to learn F#.

What's Next?

In my next video, I am going to show you how you can write F# Code if you don't have or don't like Visual Studio. Also you will be able to write F# Code from a Mac or a Linux box where Visual Studio is not available.

Comments? Questions?

Please leave a comment below.

  1. What questions do you have for me?
  2. What would you like to see me talk about next?

Get Notified of New Posts

* indicates required

Related Article