Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit focus mode

Compiler Directives

  • 2025-01-29
Feedback

In this article

This topic describes processor directives and compiler directives.

For F# Interactive (dotnet fsi) directives, seeInteractive 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#.

DirectiveDescription
#ifsymbolSupports conditional compilation. Code in the section after the#if is included if thesymbol is defined. The symbol can also be negated with!.
#elseSupports conditional compilation. Marks a section of code to include if the symbol used with the previous#if is not defined.
#endifSupports conditional compilation. Marks the end of a conditional section of code.
#[line]int,
#[line]intstring,
#[line]intverbatim-string
Indicates the original source code line and file name, for debugging. This feature is provided for tools that generate F# source code.
#nowarnwarningcodeDisables a compiler warning or warnings. To disable multiple warning numbers on the same line, separate each string by a space.
For example:#nowarn 9 42

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, andtrue andfalse have no special meaning. Symbols that you use in theif directive must be defined by the command line or in the project settings; there is nodefine 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 offunction1. WhenVERSION1 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 VERSION1let function1 x y =   printfn "x: %d y: %d" x y   x + 2 * y#elselet function1 x y =   printfn "x: %d y: %d" x y   x - 2*y#endiflet 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 whennot debugging:

#if !DEBUGlet str = "Not debugging!"#elselet str = "Debugging!"#endif

NULLABLE directive

Starting with F# 9, you can enable nullable reference types in the project:

<Nullable>enable</Nullable>

This automatically setsNULLABLE directive to the build. It's useful while initially rolling out the feature, to conditionally change conflicting code by#if NULLABLE hash directives:

#if NULLABLE let length (arg: 'T when 'T: not null) =    Seq.length arg#elselet length arg =    match arg with    | null -> -1    | s -> Seq.length s#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 fileScript1 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 fileScript1.

# 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 line25 inScript1.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNo

In this article

Was this page helpful?

YesNo