You are previewing Programming F#.

Programming F#

Cover of Programming F# by Chris Smith Published by O'Reilly Media, Inc.


The most noticeable difference between normal F# source files and F# script files is the usage of compiler directives. These are special hints and commands to the F# compiler.

General Directives

The following directives are available in any F# source program but are mostly applicable to F# scripts.


The Source Directory directive, available with __SOURCE_DIRECTORY__, returns the current directory of the executing code. This is especially helpful in scripts for determining where the script file is located. Any instance of __SOURCE_DIRECTORY__ will be replaced with a constant string as soon as the file is compiled:

C:\Program Files\Microsoft F#\v4.0>fsi.exe

Microsoft F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version, compiling for .NET Framework Version v4.0.20620

Please send bug reports to
For help type #help;;

val it : string = "C:\Program Files\Microsoft F#\v4.0"
> #q;;

C:\Program Files\Microsoft F#\v4.0>


The __SOURCE_FILE__ directive returns the current filename. When you combine this directive with __SOURCE_DIRECTORY__, you can determine the actual script being executed.

Example 9-1 runs a script that prints the __SOURCE_FILE__ to the console. Like __SOURCE_DIRECTORY__, __SOURCE_FILE__ will be replaced with a string as soon as the file is compiled.

Example 9-1. Using __SOURCE_FILE__ and __SOURCE_DIRECTORY__

C:\Program Files\Microsoft F#\v4.0>type AweomestScriptEver.fsx ...

The best content for your career. Discover unlimited learning on demand for around $1/day.