Get Started with F# in Visual Studio Code
You can write F# in Visual Studio Code with the Ionide plugin to get a great cross-platform, lightweight Integrated Development Environment (IDE) experience with IntelliSense and basic code refactorings. Visit Ionide.io to learn more about the plugin.
To begin, ensure that you have F# and the Ionide plugin correctly installed.
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.
Creating your first project with Ionide
To create a new F# project, open Visual Studio Code in a new folder (you can name it whatever you like).
Next, open the command palette (View > Command Palette) and type the following:
> F# new project
This is powered by the FORGE project.
If you don't see template options, try refreshing templates by running the following command in the Command Palette:
>F#: Refresh Project Templates.
Select "F#: New Project" by hitting Enter. This takes you to the next step, which is for selecting a project template.
classlib template and hit Enter.
Next, pick a directory to create the project in. If you leave it blank, it uses the current directory.
Finally, name your project in the final step. F# uses Pascal case for project names. This article uses
ClassLibraryDemo as the name. Once you've entered the name you want for your project, hit Enter.
If you followed the previous step, you should get the Visual Studio Code Workspace on the left-hand side to appear with the following:
- The F# project itself, underneath the
- The correct directory structure for adding packages via
- A cross-platform build script with
paket.exeexecutable that can fetch packages and resolve dependencies for you.
.gitignorefile if you wish to add this project to Git-based source control.
Writing some code
Open the ClassLibraryDemo folder. You should see the following files:
ClassLibraryDemo.fs, an F# implementation file with a class defined.
ClassLibraryDemo.fsproj, an F# project file used to build this project.
Script.fsx, an F# script file that loads the source file.
paket.references, a Paket file that specifies the project dependencies.
Script.fsx, and add the following code at the end of it:
let toPigLatin (word: string) = let isVowel (c: char) = match c with | 'a' | 'e' | 'i' |'o' |'u' | 'A' | 'E' | 'I' | 'O' | 'U' -> true |_ -> false if isVowel word. then word + "yay" else word.[1..] + string(word.) + "ay"
This function converts a word to a form of Pig Latin. The next step is to evaluate it using F# Interactive (FSI).
Highlight the entire function (it should be 11 lines long). Once it is highlighted, hold the Alt key and hit Enter. You'll notice a window pop up below, and it should show something like this:
This did three things:
- It started the FSI process.
- It sent the code you highlighted over the FSI process.
- The FSI process evaluated the code you sent over.
Because what you sent over was a function, you can now call that function with FSI! In the interactive window, type the following:
You should see the following result:
val it : string = "ananabay"
Now, let's try with a vowel as the first letter. Enter the following:
You should see the following result:
val it : string = "appleyay"
The function appears to be working as expected. Congratulations, you just wrote your first F# function in Visual Studio Code and evaluated it with FSI!
As you may have noticed, the lines in FSI are terminated with
;;. This is because FSI allows you to enter multiple lines. The
;; at the end lets FSI know when the code is finished.
Explaining the code
If you're not sure about what the code is actually doing, here's a step-by-step.
As you can see,
toPigLatin is a function that takes a word as its input and converts it to a Pig-Latin representation of that word. The rules for this are as follows:
If the first character in a word starts with a vowel, add "yay" to the end of the word. If it doesn't start with a vowel, move that first character to the end of the word and add "ay" to it.
You may have noticed the following in FSI:
val toPigLatin : word:string -> string
This states that
toPigLatin is a function that takes in a
string as input (called
word), and returns another
string. This is known as the type signature of the function, a fundamental piece of F# that's key to understanding F# code. You'll also notice this if you hover over the function in Visual Studio Code.
In the body of the function, you'll notice two distinct parts:
An inner function, called
isVowel, that determines if a given character (
c) is a vowel by checking if it matches one of the provided patterns via Pattern Matching:
let isVowel (c: char) = match c with | 'a' | 'e' | 'i' |'o' |'u' | 'A' | 'E' | 'I' | 'O' | 'U' -> true |_ -> false
if..then..elseexpression that checks if the first character is a vowel, and constructs a return value out of the input characters based on if the first character was a vowel or not:
if isVowel word. then word + "yay" else word.[1..] + string(word.) + "ay"
The flow of
toPigLatin is thus:
Check if the first character of the input word is a vowel. If it is, attach "yay" to the end of the word. Otherwise, move that first character to the end of the word and add "ay" to it.
There's one final thing to notice about this: there's no explicit instruction to return from the function, unlike many other languages out there. This is because F# is Expression-based, and the last expression in the body of a function is the return value. Because
if..then..else is itself an expression, the body of the
then block or the body of the
else block will be returned depending on the input value.
Moving your script code into the implementation file
The previous sections in this article demonstrated a common first step in writing F# code: writing an initial function and executing it interactively with FSI. This is known as REPL-driven development, where REPL stands for "Read-Evaluate-Print Loop". It's a great way to experiment with functionality until you have something working.
The next step in REPL-driven development is to move working code into an F# implementation file. It can then be compiled by the F# compiler into an assembly that can be executed.
To begin, open
ClassLibraryDemo.fs. You'll notice that some code is already in there. Go ahead and delete the class definition, but make sure to leave the
namespace declaration at the top.
Next, create a new
PigLatin and copy the
toPigLatin function into it as such:
namespace ClassLibraryDemo module PigLatin = let toPigLatin (word: string) = let isVowel (c: char) = match c with | 'a' | 'e' | 'i' |'o' |'u' | 'A' | 'E' | 'I' | 'O' | 'U' -> true |_ -> false if isVowel word. then word + "yay" else word.[1..] + string(word.) + "ay"
Next, open the
Script.fsx file again, and delete the entire
toPigLatin function, but make sure to keep the following two lines in the file:
#load "ClassLibraryDemo.fs" open ClassLibraryDemo
Select both lines of text and press Alt+Enter to execute these lines in FSI. These will load the contents of the Pig Latin library into the FSI process and
ClassLibraryDemo namespace so that you have access to the functionality.
Next, in the FSI window, call the function with the
PigLatin module that you defined earlier:
> PigLatin.toPigLatin "banana";; val it : string = "ananabay" > PigLatin.toPigLatin "apple";; val it : string = "appleyay"
Success! You get the same results as before, but now loaded from an F# implementation file. The major difference here is that F# source files are compiled into assemblies that can be executed anywhere, not just in FSI.
In this article, you've learned:
- How to set up Visual Studio Code with Ionide.
- How to create your first F# project with Ionide.
- How to use F# Scripting to write your first F# function in Ionide and then execute it in FSI.
- How to migrate your script code to F# source and then call that code from FSI.
You're now equipped to write much more F# code using Visual Studio Code and Ionide.
Here are a few ways you can troubleshoot certain problems that you might run into:
- To get the code editing features of Ionide, your F# files need to be saved to disk and inside of a folder that is open in the Visual Studio Code workspace.
- If you've made changes to your system or installed Ionide prerequisites with Visual Studio Code open, restart Visual Studio Code.
- Check that you can use the F# compiler and F# interactive from the command line without a fully-qualified path. You can do so by typing
fscin a command line for the F# compiler, and
fsharpifor the Visual F# tools on Windows and Mono on Mac/Linux, respectively.
- If you have invalid characters in your project directories, Ionide might not work. Rename your project directories if this is the case.
- If none of the Ionide commands are working, check your Visual Studio Code keybindings to see if you're overriding them by accident.
- If Ionide is broken on your machine and none of the above has fixed your problem, try removing the
ionide-fsharpdirectory on your machine and reinstall the plugin suite.
Ionide is an open source project built and maintained by members of the F# community. Please report issues and feel free to contribute at the Ionide-VSCode: FSharp GitHub repository.
If you have an issue to report, please follow the instructions for getting logs to use when reporting an issue.
You can also ask for further help from the Ionide developers and F# community in the Ionide Gitter channel.
To learn more about F# and the features of the language, check out Tour of F#.
جارٍ تحميل الملاحظات...