Jun 26

C# from F# ? using C# and the .NET APIs from F#

Assemblies written in C# and other .NET languages can be directly accessed from F#. This makes F# an amazingly powerful language given the huge number of libraries available. This section describes how to use various .NET constructs from F#. A working knowledge of C# or another .NET language is assumed.

Longer examples of interoperability can be found in the samples that come with the F# compiler.

The Basics


Jun 26

Using F# Interactive (FSI.EXE)

Jun 26

Quick Tour: Some Basics

Literal Constants are much as in other languages.

F# Interactive is the name of the command line interactive shell for F#. It runs on .NET 2.0 (Visual Studio 2005), .NET 1.x (Visual Studio 2003) and Mono. You can also use it as a tool window in Visual Studio 2005. Run it using bin\fsi.exe. The WebCrawl, TermWalker and DirectX 3D Demo give examples of how to script. Note you can paste line-by-line into the top-level environment, or else use F# Interactive in Visual Studio or another shell-hosting environment such as those available in Emacs. You can also often compile scripts using fsc.exe.

Inputs to fsi.exe are terminated with ;; (nothing happens until you type this!). You generally don’t need to enter this when using F# from Visual Studio as it is added automatically each time you send text for execution. This applies particularly when using the light syntax option syntax #use ;; load the given file as if it had been entered from the command line #load ;; compile the given file as a named module, then load #time;; toggle timing on/off. #quit;; quit the session.

The first three of these can also be used directly in F# source code files. It is also handy to quote filenames using ‘verbatim strings’, e.g.

    #r @"c:Program Filesfsharp-ILX-VERSIONbinabsil.dll";;

When you type an expression into F# Interactive, the last such expression is assigned into the variable it. This is purely an artefact of typing entries into F# Interactive. If you compile the same code with the command-line compiler or when loading a file into F# Interactive as a compilation unit the expression will simply be executed and its result discarded.

Using F# Interactive in Visual Studio

F# comes with a Visual Studio AddIn that lets you use ‘F# Interactive’ for use as a tool Window in Visual Studio 2005. The tool window is a Visual Studio AddIn, and should be managed via the AddIn Manager menu option found under the ‘Tools’ menu. You can also start up F# Interactive simply using ‘Alt-Enter’ (Note: this key must currently be bound manually on the Japanese edition of Visual Studio 2005)

To start the session, go to the AddIn manager and select ‘F# Interactive for Visual Studio’. If it is already selected, then unselect it, close and reopen the AddIn manager and reselect it. Either way a new window should appear with an F# Interactive session.

The session is global to the instance of Visual Studio and text can be sent from any F# source code file (.fs, .fsi, .ml, .mli). Use the ‘Alt-Return’ combination to evaluate selected text, and ‘Alt-Quote’ (i.e. Alt-’) to evaluate the current line of text without selecting it. History is available with the up/down arrows. These shortcuts may change in future releases.

File names are resolve relative to the scripts containing the reference. Visual F# Interactive sets the line and directory context of the session each time an entry is sent from Visual Studio.

To troubleshoot an installation:

  • Check fsi.exe starts up at the command prompt, including entering simple programs.
  • Check the ‘bin’ directory of your F# installation (e.g. FSharp-ILX-VERSION\bin) is listed in the entries in your AddIn search path under ‘Tools’, ‘Options’ in Visual Studio.

F# Command Line - using the compiler

The fsc.exe compiler is a traditional command-line compiler with a set of command-line options similar to both the OCaml compiler and the C# compiler. Detailed help can be found by running “fsc — help”. Some of the common command-line flags are shown below, along with detailed instructions on the How to statically link the F# libary using the “–standalone” option. Also see the notes on using F# in conjunction with alternative CLI implementations.

The most common command-line options (FSC.EXE)

  • -o Name the output file.
  • -I Specify a directory for the search path for referenced assemblies.
  • -g Produce debugging information.
  • -O3 Turn on maximal optimization.
  • -a Produce a DLL.
  • -r Reference an F# or .NET DLL.
  • –keyfile Sign an assembly using the given keyfile. This must be done when creating DLLs that will be shared by multiple programs and installed in the “Global Assembly Cache” on target machines.