Compiler Directives

This topic describes processor directives and compiler directives.

For F# Interactive (dotnet fsi) directives, see Interactive Programming with F#.

Preprocessor Directives

A preprocessor directive is prefixed with the # symbol and appears on a line by itself. It is interpreted by the preprocessor, which runs before the compiler itself.

The following table lists the preprocessor directives that are available in F#.

Directive Description
#if symbol Supports conditional compilation. Code in the section after the #if is included if the symbol is defined. The symbol can also be negated with !.
#else Supports conditional compilation. Marks a section of code to include if the symbol used with the previous #if is not defined.
#endif Supports conditional compilation. Marks the end of a conditional section of code.
#[line] int,
#[line] int string,
#[line] int verbatim-string
Indicates the original source code line and file name, for debugging. This feature is provided for tools that generate F# source code.
#nowarn warningcode Disables a compiler warning or warnings. To disable a warning, find its number from the compiler output and include it in quotation marks. Omit the "FS" prefix. To disable multiple warning numbers on the same line, include each number in quotation marks, and separate each string by a space.
For example: #nowarn "9" "40"

The effect of disabling a warning applies to the entire file, including portions of the file that precede the directive.|

Conditional Compilation Directives

Code that is deactivated by one of these directives appears dimmed in the Visual Studio Code Editor.

Note

The behavior of the conditional compilation directives is not the same as it is in other languages. For example, you cannot use Boolean expressions involving symbols, and true and false have no special meaning. Symbols that you use in the if directive must be defined by the command line or in the project settings; there is no define preprocessor directive.

The following code illustrates the use of the #if, #else, and #endif directives. In this example, the code contains two versions of the definition of function1. When VERSION1 is defined by using the -define compiler option, the code between the #if directive and the #else directive is activated. Otherwise, the code between #else and #endif is activated.

#if VERSION1
let function1 x y =
   printfn "x: %d y: %d" x y
   x + 2 * y
#else
let function1 x y =
   printfn "x: %d y: %d" x y
   x - 2*y
#endif

let result = function1 10 20

There is no #define preprocessor directive in F#. You must use the compiler option or project settings to define the symbols used by the #if directive.

Conditional compilation directives can be nested. Indentation is not significant for preprocessor directives.

You can also negate a symbol with !. In this example, a string's value is something only when not debugging:

#if !DEBUG
let str = "Not debugging!"
#else
let str = "Debugging!"
#endif

Line Directives

When building, the compiler reports errors in F# code by referencing line numbers on which each error occurs. These line numbers start at 1 for the first line in a file. However, if you are generating F# source code from another tool, the line numbers in the generated code are generally not of interest, because the errors in the generated F# code most likely arise from another source. The #line directive provides a way for authors of tools that generate F# source code to pass information about the original line numbers and source files to the generated F# code.

When you use the #line directive, file names must be enclosed in quotation marks. Unless the verbatim token (@) appears in front of the string, you must escape backslash characters by using two backslash characters instead of one in order to use them in the path. The following are valid line tokens. In these examples, assume that the original file Script1 results in an automatically generated F# code file when it is run through a tool, and that the code at the location of these directives is generated from some tokens at line 25 in file Script1.

# 25
#line 25
#line 25 "C:\\Projects\\MyProject\\MyProject\\Script1"
#line 25 @"C:\Projects\MyProject\MyProject\Script1"
# 25 @"C:\Projects\MyProject\MyProject\Script1"

These tokens indicate that the F# code generated at this location is derived from some constructs at or near line 25 in Script1.

See also