Skip to content

Document Automatically Defined Compiler Symbols in F# Compiler Directives #45607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions docs/fsharp/language-reference/compiler-directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,46 @@ let str = "Debugging!"
#endif
```

### Automatically Defined Symbols

In addition to symbols explicitly defined via project settings or command-line options, **F# projects inherit a set of predefined symbols** based on the SDK, target framework, and build configuration.

#### Debug, Tracing, and Nullable Settings

| Symbol | Description |
|-----------|-------------|
| `DEBUG` | Defined in debug builds when compiling with the `Debug` configuration. |
| `TRACE` | Defined when tracing is enabled in the project settings. |
| `NULLABLE` | Defined when `<Nullable>enable</Nullable>` is set in the project file. |

#### Target Framework Symbols
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am sorry for the misunderstanding.
Could those be instead solved by a link to the specific SDK documentation?

This is not being defined by F# and it does naturally evolve. If we keep a duplicate here, it would also require maintenace.

I think linking this very page https://learn.microsoft.com/en-us/dotnet/standard/frameworks#preprocessor-symbols would be best when it comes to completeness (see how many there are overall)


| Symbol | Description |
|--------|-------------|
| `NET`, `NETSTANDARD`, `NETCOREAPP` | General indicators of the .NET target type. |
| `NET5_0_OR_GREATER`, `NET6_0_OR_GREATER`, `NET7_0_OR_GREATER` | Defined for specific .NET versions. |
| `NETCOREAPP3_1_OR_GREATER`, `NETCOREAPP2_0_OR_GREATER` | Defined for .NET Core versions. |

#### Build Configuration Symbols

| Symbol | Description |
|-----------|-------------|
| `Release` | Defined in release builds. |
| `COMPILED` | Used in compiled projects. |

For example, you can use these symbols in conditional compilation:

```fsharp
#if DEBUG
printfn "Debugging mode is enabled."
#else
printfn "Release mode."
#endif
```

For a full list of predefined symbols, refer to the official documentation:
[.NET SDK Predefined Compilation Symbols](https://learn.microsoft.com/dotnet/fsharp/tools/fsharp-interactive/#interactive-and-compiled-preprocessor-directives).

## NULLABLE directive

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