Skip to content

Commit baa0858

Browse files
CopilotBillWagner
andcommitted
Add predefined compiler symbols documentation
Co-authored-by: BillWagner <[email protected]>
1 parent 44cd225 commit baa0858

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

docs/fsharp/language-reference/compiler-directives.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,58 @@ There is no `#define` compiler directive in F#. You must use the compiler option
5353

5454
Conditional compilation directives can be nested. Indentation is not significant for compiler directives.
5555

56+
## Predefined symbols
57+
58+
The F# compiler and build system automatically define several symbols that can be used for conditional compilation.
59+
60+
### Build configuration symbols
61+
62+
The following symbols are defined based on your build configuration:
63+
64+
- `DEBUG`: Defined when compiling in Debug mode. In the project system, the `DEBUG` symbol is automatically defined in the Debug configuration, but not in the Release configuration. This symbol is commonly used with assertions and diagnostic code. For more information, see [Assertions](assertions.md).
65+
- `TRACE`: Defined for builds that enable tracing. Like `DEBUG`, this symbol is typically defined in Debug configurations but can also be enabled in Release configurations.
66+
67+
You can override these values using the [`-define` compiler option](compiler-options.md) or project settings.
68+
69+
### Compilation mode symbols
70+
71+
The following symbols distinguish between different compilation modes:
72+
73+
- `COMPILED`: Defined when compiling code with the F# compiler. This symbol is useful when you need code to behave differently in compiled assemblies versus F# Interactive sessions.
74+
- `INTERACTIVE`: Defined when compiling or executing code in F# Interactive (`dotnet fsi`), including both interactive sessions and script execution. This allows you to write code that works differently when running interactively.
75+
76+
For more information about using these symbols in scripts, see [Interactive Programming with F#](../tools/fsharp-interactive/index.md).
77+
78+
Example:
79+
80+
```fsharp
81+
#if INTERACTIVE
82+
// Code specific to F# Interactive
83+
#r "nuget: Newtonsoft.Json"
84+
#endif
85+
86+
#if COMPILED
87+
// Code specific to compiled assemblies
88+
open System.Configuration
89+
#endif
90+
```
91+
92+
### Target framework symbols
93+
94+
The build system also defines preprocessor symbols for different target frameworks in SDK-style projects. These symbols are useful when creating libraries or applications that target multiple .NET versions.
95+
96+
[!INCLUDE [Preprocessor symbols](~/includes/preprocessor-symbols.md)]
97+
98+
For example, you can use these symbols to conditionally compile code based on the target framework:
99+
100+
```fsharp
101+
#if NET6_0_OR_GREATER
102+
// Use .NET 6+ specific APIs
103+
#else
104+
// Use alternative implementation for older frameworks
105+
#endif
106+
```
107+
56108
## NULLABLE directive
57109

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

0 commit comments

Comments
 (0)