There are many different ways to run an F# script (.fsx) file. All of them are covered here! For F#ers on Windows here is the official documentation MSDN: FSharp Interactive. Leave a comment with your favorite!
Text Editors
The following steps apply to the excellent plugin Ionide for VS Code or Atom. If Ionide is not installed on your machine, here’s some help F#: Getting started with IDEs and Text Editors 1: Sending the entire file: CMD-SHIFT-P -> FSI: Send file
- The entire file will be loaded and executed.
- Reliability is guaranteed as the whole script is reloaded.
- May not be the fastest; no shortcut
2: By selection (highlight a block of code first): CMD-SHIT-P: FSI: send selection
Note the shortcut
- Will compile and execute the selection
- One of the fastest methods with the shortcut
- Can be error prone, since the F#er must remember to send earlier code segments
- Type inference can be negatively affected. Eg a function with statically resolved type parameter sent to REPL without usage.
- Parent functions must be reloaded if a called function is updated.
3: By line (Put cursor on desired line first): CMD-SHIT-P: FSI: send line
- Will compile and execute the line
- One of the fastest methods with the shortcut
- Can be error prone, since the F#er must remember to send earlier code segments
- Type inference can be negatively affected. Eg a function with statically resolved type parameter sent to REPL without usage.
- Parent functions must be reloaded if a called function is updated.
IDEs: Visual Studio For Mac
4: Sending the entire file: Right click -> Send current file to F# Interactive
- The entire file will be loaded and executed.
- Reliability is guaranteed as the whole script is reloaded.
- May not be the fastest; no shortcut
UPDATE:
Thanks to Jason Imison for pointing out that it is possible to set a shortcut.
- Open
Preferences
- On the left select
Key Bindings
- Filter using the search box
send current file
- Select the item, and set a shortcut in the text box
Apply
->Ok
5: By selection (highlight a block of code first): Right Click -> ‘Send selection to F# Interactive’
- Will compile and execute the selection
- One of the fastest methods with the shortcut
- Can be error prone, since the F#er must remember to send earlier code segments
- Type inference can be negatively affected. Eg a function with statically resolved type parameter sent to REPL without usage.
- Parent functions must be reloaded if a called function is updated.
6: By line (Put cursor on desired line first): Right Click -> Send line to F# Interactive
- Will compile and execute the line
- One of the fastest methods with the shortcut
- Can be error prone, since the F#er must remember to send earlier code segments
- Type inference can be negatively affected. Eg a function with statically resolved type parameter sent to REPL without usage.
- Parent functions must be reloaded if a called function is updated.
IDEs: Visual Studio For Windows
7: By Selection: Right Click -> Execute in Interactive (shortcut is Alt + Enter
)
- Will compile and execute the selection
- One of the fastest methods with the shortcut
- Can be error prone, since the F#er must remember to send earlier code segments
- Type inference can be negatively affected. Eg a function with statically resolved type parameter sent to REPL without usage.
- Parent functions must be reloaded if a called function is updated.
8: By line (Put cursor on desired line first): Shortcut: Alt + '
.
- Will compile and execute the line
- One of the fastest methods with the shortcut
- Can be error prone, since the F#er must remember to send earlier code segments
- Type inference can be negatively affected. Eg a function with statically resolved type parameter sent to REPL without usage.
- Parent functions must be reloaded if a called function is updated.
If the shortcuts are not correct, Mathias has a discovered a quick solution
Terminal
open a terminal window. The command to start an F# REPL is fsharpi
9: Copy and Paste: fsharpi
-> paster in code -> type ;;
-> press enter/return
- Will compile and execute the selection
- One of the fastest methods with the shortcut
- Can be error prone, since the F#er must remember to send earlier code segments
- Type inference can be negatively affected. Eg a function with statically resolved type parameter sent to REPL without usage cannot be resolved.
- Parent functions must be reloaded if a called function is updated.
10: Load entire file. fsharpi --load:Script.fsx
Script.fsx must be in the current directory
- The entire file will be loaded into a namespace of the filename eg Script
- Reliability is guaranteed as the whole script is reloaded.
- Requires compiling the entire file each time
- Useful if execution of the script needs to be controlled/delayed.
11: Load and run entire file. fsharpi --use:Script.fsx
Script.fsx must be in the current directory
- The entire file will be loaded, namespace opened and executed
- Reliability is guaranteed as the whole script is reloaded.
- Requires compiling the entire file each time
UPDATE: Thanks to Jason Imison for point out the following: 13: Shebang:
- Add the following to the top of the script file:
#!/usr/bin/env fsharpi --exec
- Make the file executable:</code>chmod +x Script.fsx
- Execute: ./Script.fsxa
- Only works on Mac/Linux
- The entire file will be loaded, namespace opened and executed
- Reliability is guaranteed as the whole script is reloaded.
- Requires compiling the entire file each time
Rider: For the sake of completeness
12: By Selection: Shortcut option + enter
- Will compile and execute the selection
- One of the fastest methods with the shortcut
- can be error prone, since the F#er must remember to send earlier code segments
- type inference can be negatively affected. Eg a function with statically resolved type parameter sent to REPL without usage.
- Parent functions must be reloaded if a called function is updated.
If a method is missing leave a comment!