GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||
GeezerVison is showing learn.microsoft.com — F# Interactive (dotnet) Reference - .NET Note Access to this page requires authorization. You can try signing in or changing directories. Access to this page requires authorization. You can try changing directories. Interactive programming with F#F# Interactive (dotnet fsi) is used to run F# code interactively at the console, or to execute F# scripts. In other words, F# interactive executes a REPL (Read, Evaluate, Print Loop) for F#. To run F# Interactive from the console, run Note If you intend to use F# interactive under .NET Framework runtime, you'll need the Visual Studio Build Tools or an edition of Visual Studio installed, and invoke the Tooling supports defining version F# Interactive runtime:
For information about available command-line options, see F# Interactive Options. Executing code directly in F# InteractiveBecause F# Interactive is a REPL (read-eval-print loop), you can execute code interactively in it. Here is an example of an interactive session after executing
You'll notice two main things:
F# Interactive also supports multi-line input. You just need to terminate your submission with a double semicolon (
The code's formatting is preserved, and there is a double semicolon ( Core build and asynchronous disposalFor asynchronous disposal in task expressions, update to the F# 11 toolset with the modern Core packaging fix. An application package reference doesn't change FSI's bundled Core. Display version informationStarting with F# 11, enter
Include this output when you report a compiler or scripting problem. Scripting with F#Evaluating code interactively in F# Interactive can be a great learning tool, but you'll quickly find that it's not as productive as writing code in a normal editor. To support normal code editing, you can write F# scripts. Scripts use the file extension .fsx. Instead of compiling source code and then later running the compiled assembly, you can just run dotnet fsi and specify the filename of the script, and F# Interactive reads the code and executes it in real time. For example, consider the following script called
When this file is created in your machine, you can run it with
Executing Scripts with a ShebangTo make F# scripts executable without explicitly invoking For example, create a script file called
This feature allows for a more seamless experience when working with F# scripts in environments like Linux and macOS. F# scripting is natively supported in Visual Studio and Visual Studio Code. Referencing packages in F# InteractiveNote Package management system is extensible, see more about the plugins and extension mechanism. Since 5.0 release of the language, F# Interactive supports referencing packages through an extensibility mechanism; out of the box, it can reference NuGet packages with the
If a version is not specified, the highest available non-preview package is taken. To reference a specific version, introduce the version via a comma. This can be handy when referencing a preview version of a package. For example, consider this script using a preview version of DiffSharp:
By default,
Important Referencing packages with NuGet documentation describes best practices for securing your software supply chain. Specifying a package sourceYou can also specify a package source with the
This tells the resolution engine to take into account the remote and/or local sources added to a script. You can specify as many package sources as you like in a script. Important Relative paths aren't currently supported with the Workaround: You can programmatically construct an absolute path using
Note There's currently a limitation for scripts that use framework references (e.g. To load additional extensions beside those shipped with the SDK and/or with your tooling, use the Referencing assemblies on disk with F# interactiveAlternatively, if you have an assembly on disk and wish to reference that in a script, you can use the
Once compiled, you can reference it in a file called
The output is as follows:
You can specify as many assembly references as you like in a script. Loading other scriptsWhen scripting, it can often be helpful to use different scripts for different tasks. Sometimes you may want to reuse code from one script in another. Rather than copy-pasting its contents into your file, you can simply load and evaluate it with Consider the following
And the consuming file,
You can evaluate
You can specify as many Note The
Using the
|
| Directive | Description |
|---|---|
#r "nuget:..." |
References a package from NuGet |
#r "extname:..." |
Reference a package from extname extension[^1] (such as paket) |
#r "assembly-name.dll" |
References an assembly on disk |
#load "file-name.fsx" |
Reads a source file, compiles it, and runs it. |
#help |
Displays information about available directives or documentation for specific functions. |
#I |
Specifies an assembly search path in quotation marks. |
#quit |
Terminates an F# Interactive session. |
#time on or #time off |
By itself, #time toggles whether to display performance information. When it is on, F# Interactive measures real time, CPU time, and garbage collection information for each section of code that is interpreted and executed. |
[^1]: More about F# Interactive extensions.
When you specify files or paths in F# Interactive, a string literal is expected. Therefore, files and paths must be in quotation marks, and the usual escape characters apply. You can use the @ character to cause F# Interactive to interpret a string that contains a path as a verbatim string. This causes F# Interactive to ignore any escape characters.
For other cases, quotation marks are optional, starting with F# 9.
The #help directive now supports displaying documentation for specific functions. You can pass the name of the function directly to retrieve details.
#help List.map;;
The output is as follows:
Description:
Builds a new collection whose elements are the results of applying the given function
to each of the elements of the collection.
Parameters:
- mapping: The function to transform elements from the input list.
- list: The input list.
Returns:
The list of transformed elements.
Examples:
let inputs = [ "a"; "bbb"; "cc" ]
inputs |> List.map (fun x -> x.Length)
// Evaluates to [ 1; 3; 2 ]
Full name: Microsoft.FSharp.Collections.ListModule.map
Assembly: FSharp.Core.dll
This enhancement makes it easier to explore and understand F# libraries interactively.
For more details, refer to the official devblog.
When you compile code in F# Interactive, whether you are running interactively or running a script, the symbol INTERACTIVE is defined. When you compile code in the compiler, the symbol COMPILED is defined. Thus, if code needs to be different in compiled and interactive modes, you can use these preprocessor directives for conditional compilation to determine which to use. For example:
#if INTERACTIVE
// Some code that executes only in FSI
// ...
#endif
To run F# Interactive through Visual Studio, you can click the appropriate toolbar button labeled F# Interactive, or use the keys Ctrl+Alt+F. Doing this will open the interactive window, a tool window running an F# Interactive session. You can also select some code that you want to run in the interactive window and hit the key combination Alt+Enter. F# Interactive starts in a tool window labeled F# Interactive. When you use this key combination, make sure that the editor window has the focus.
Whether you are using the console or Visual Studio, a command prompt appears and the interpreter awaits your input. You can enter code just as you would in a code file. To compile and execute the code, enter two semicolons (;;) to terminate a line or several lines of input.
F# Interactive attempts to compile the code and, if successful, it executes the code and prints the signature of the types and values that it compiled. If errors occur, the interpreter prints the error messages.
Code entered in the same session has access to any constructs entered previously, so you can build up programs. An extensive buffer in the tool window allows you to copy the code into a file if needed.
When run in Visual Studio, F# Interactive runs independently of your project, so, for example, you cannot use constructs defined in your project in F# Interactive unless you copy the code for the function into the interactive window.
You can control the F# Interactive command-line arguments (options) by adjusting the settings. On the Tools menu, select Options..., and then expand F# Tools. The two settings that you can change are the F# Interactive options and the 64-bit F# Interactive setting, which is relevant only if you are running F# Interactive on a 64-bit machine. This setting determines whether you want to run the dedicated 64-bit version of fsi.exe or fsianycpu.exe, which uses the machine architecture to determine whether to run as a 32-bit or 64-bit process.
| Title | Description |
|---|---|
| F# Interactive Options | Describes command-line syntax and options for the F# Interactive, fsi.exe. |
Was this page helpful?