This topic describes processor directives and compiler 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#.
||Supports conditional compilation. Code in the section after the
||Supports conditional compilation. Marks a section of code to include if the symbol used with the previous
||Supports conditional compilation. Marks the end of a conditional section of code.|
||Indicates the original source code line and file name, for debugging. This feature is provided for tools that generate F# source code.|
||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.
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
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
#endif directives. In this example, the code contains two versions of the definition of
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
#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
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
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
# 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
Compiler directives resemble preprocessor directives, because they are prefixed with a # sign, but instead of being interpreted by the preprocessor, they are left for the compiler to interpret and act on.
The following table lists the compiler directive that is available in F#.
||Enables or disables lightweight syntax, for compatibility with other versions of ML. By default, lightweight syntax is enabled. Verbose syntax is always enabled. Therefore, you can use both lightweight syntax and verbose syntax. The directive
For interpreter (fsi.exe) directives, see Interactive Programming with F#.
Send feedback about: