O'Reilly logo

Programming F# by Chris Smith

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Directives

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.

__SOURCE_DIRECTORY__

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 1.9.8.0, compiling for .NET Framework Version v4.0.20620

Please send bug reports to fsbugs@microsoft.com
For help type #help;;

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

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

__SOURCE_FILE__

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required