Cover by Chris Smith

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

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

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required