Creating my first F# code

Category: visual studio fsharp


DaFonseca on Thu, 30 May 2013 03:53:15

I am on VStudio 2012, but when I try to open a new project (or file) in F#, I just have two options: F# library, and F# tutorial (which I already ran).

My question is: how can I open a new (blank) code to begin coding?

I think, I must have F# script or application as options, but how?



Plepilov on Thu, 30 May 2013 12:26:22

If you open F# library VS creates pretty basic project with two files: 3-line class definition and empty F# script file.

You can modify this project as you wish (add/remove files) etc.

Or you are asking about something else?

DaFonseca on Thu, 30 May 2013 13:36:06

In fact, i did this, So now i have a Library5 project with References, a source file.fs and a Script file ("#load "Library1.fs"

open Library6 ")

So, on on Script.fsx (//Define your librarya scripting here part) I ve trie some lines. When I run it:

It says: A project without an Output Type of Class |library cannot be started directly. In order to debug this project, add an executable project to this solution which references to library project. Set the exec project as the startt up project. 

Is there something like EMACS on F# where i could code and interact with it on a command line?? where is it?


Plepilov on Thu, 30 May 2013 13:49:54

Scripts are mostly for developing/testing the code with F# interactive window. To run the code in script you select it and hit 'Alt-Enter' or right click on selection and select 'Execute In Interactive'. Results will appear in Visual Studio window called 'F# interactive' which is usually opens at he bottom of Visual Studio. See for reference.

If you want to create F# running application you should choose "F# Application" from VS 'New Project' dialog. It will be created with proper main function as an entry point.

Alternatively you could also select 'Output type:' - 'Console application' in Library project's 'Properties' window. In this case you'll have to create main function yourself.

Keep in mind that when you run/debug the application in VS it will execute only code in F# source files (*.fs). So you have to copy tested code from your script to source file.


DaFonseca on Thu, 30 May 2013 14:17:28

I got your point and now I am able to run (using script and execute in interactive).

Are there any specific tutorial on F# application/Output type/console application.

Ineed to understand these mechanisms.

Plepilov on Thu, 30 May 2013 15:13:18

You can start right from this topic:

"Using Visual Studio to Write F# Programs"

DaFonseca on Thu, 30 May 2013 16:03:42

Oh That is helpfull

Reading it I have one question regarding this part below: the problem is : File-> New file-> do not give me any F# option (only C#) Do I have to add something?

There are two basic styles of development that Visual F# supports: scripts and projects. You can use an F# script when you just want to run a small amount of code that you do not want to make into a permanent application. You use a project when you are creating a more permanent application.

To create and run an F# script, you do not need to create a project. To create an F# script, on the File menu, point to New and then click File. In the New File dialog box, select Script in the Installed Templates list, and then select F# Script File. Scripts are designed for execution with F# Interactive (fsi.exe). For more information, see F# Interactive (fsi.exe) Reference.

Plepilov on Thu, 30 May 2013 16:11:36

I'm not sure.

Actually in my installation of VS 2012 I have 'F# Script File' as an option both in 'General' and in "Script' template windows.

What version of Visual Studio 2012 are you using?

DaFonseca on Thu, 30 May 2013 16:20:53

Microsoft V S Express 2012 for web

version 11.0.60315.01 Update2

DaFonseca on Thu, 30 May 2013 16:27:31

The solution I found was open a new file as a text file and then add a new iem as a F# script file.

Plepilov on Thu, 30 May 2013 16:32:41

Yes that could be the solution for VS Express 2012 Web.

DaFonseca on Mon, 03 Jun 2013 15:34:44

How can I have F# application (along with F# Library and Tutorial) when creating a new project??(below)


To create an F# project, on the File menu, point to New and then click Project. In the New Project dialog box, select a project template. Visual Studio provides templates that enable you to create projects that already have all the basic elements and settings that support applications and libraries. In F#, you use the F# Library or F# Portable Library project template when you are creating a DLL and the F# Application project when you are creating an executable file. You should use the F# Portable Library project if your library will be consumed by applications that will run on platforms that don't support the full .NET Framework, such as a Windows Store app or a Silverlight app. You can also target Silverlight by using the Silverlight project template.

Plepilov on Mon, 03 Jun 2013 20:07:15

I believe that this type of project is not supported in F# Tools  for VS Express 2012 Web.

DaFonseca on Mon, 03 Jun 2013 20:09:03

I see, but How am I supposed to create a new F# project on VS2012?


Plepilov on Tue, 04 Jun 2013 12:21:10

Using VS Express 2012 Web with F# tools installed you can create F# library project. Also you can manually create *.fs file with main function:

let main args =
    printfn "Arguments passed to function : %A" args
    // Return 0. This indicates success.

and change the project type to 'Console Application' in project properties.

VS 2012 Professional and higher versions support creation of any type of F# project.


DaFonseca on Tue, 04 Jun 2013 16:49:04

One more question:

what would be the difference between open and #load.

As I understand I must "open" all libraries which are F# "core" such as System.Net, etc..

in case I want to use any other code , I  must copy it to references and use #load.. Is that corect?

is there any video dealing with it?

Plepilov on Tue, 04 Jun 2013 17:09:13

Here is a good brief overview of F# scripting.

Shortly, in script file:

1) you adds the reference to a .NET library (if it's not core library) using #r command

#r "MyLibrary.dll"

2) you opens .NET namespace using 'open' keyword in order to use types from this namespace:

open System.IO 

3) #load directive is used to load types and functions from F# source file "*.fs" in order to use them in the script. In this case you also may need to open the namespace or module defined in this source file

#load "MyFile.fs"

open MyNamespace