Skip to content

Commit 19badc5

Browse files
committed
update for RFC FS-1146 (scoped nowarn)
and for extended if grammar
1 parent 7f30eb5 commit 19badc5

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,39 +19,45 @@ The following table lists the preprocessor directives that are available in F#.
1919

2020
|Directive|Description|
2121
|---------|-----------|
22-
|`#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 `!`.|
23-
|`#else`|Supports conditional compilation. Marks a section of code to include if the symbol used with the previous `#if` is not defined.|
22+
|`#if` *if-expression*|Supports conditional compilation. Code in the section after the `#if` is included if the *if-eyxpression* evaluates to `defined` (see below).|
23+
|`#else`|Supports conditional compilation. Marks a section of code to include if the symbol used with the previous `#if` does not evaluate to `defined`.|
2424
|`#endif`|Supports conditional compilation. Marks the end of a conditional section of code.|
2525
|`#`[line] *int*,<br/>`#`[line] *int* *string*,<br/>`#`[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.|
26-
|`#nowarn` *warningcode*|Disables a compiler warning or warnings. To disable multiple warning numbers on the same line, separate each string by a space. <br/> For example: `#nowarn 9 42`|
26+
|`#nowarn` *warningcode*|Disables a compiler warning or warnings. To disable multiple warning numbers on the same line, separate each string by a space. <br/> For example: `#nowarn 9 42`<br/> The warning is disabled until eof or until a `#warnon` directive for that same warning number is foud.|
27+
|`#warnon` *warningcode*|Enables a compiler warning (or warnings) that was disabled by a compiler option or by a `#nowarn` directive..<br/> The warning is enabled until eof or until a `#nowarn` directive for that same warning number is found.|
2728

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

3030
## Conditional Compilation Directives
3131

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

34-
> [!NOTE]
35-
> 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.
36-
3734
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](./compiler-options.md), the code between the `#if` directive and the `#else` directive is activated. Otherwise, the code between `#else` and `#endif` is activated.
3835

3936
[!code-fsharp[Main](~/samples/snippets/fsharp/lang-ref-2/snippet7301.fs)]
4037

41-
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.
42-
43-
Conditional compilation directives can be nested. Indentation is not significant for preprocessor directives.
44-
45-
You can also negate a symbol with `!`. In this example, a string's value is something only when _not_ debugging:
38+
The `#if` directive also accepts logical expressions:
4639

4740
```fsharp
48-
#if !DEBUG
49-
let str = "Not debugging!"
50-
#else
51-
let str = "Debugging!"
41+
#if SILVERLIGHT || COMPILED && (NETCOREFX || !DEBUG)
5242
#endif
5343
```
5444

45+
The following expressions can be used.
46+
47+
| if-expr | evaluation |
48+
| --- | --- |
49+
| `if-expr1 \|\| if-expr2` | `defined` if `if-expr1` or `if-expr2` is `defined`. |
50+
| `if-expr1 && if-expr2` | `defined` if `if-expr1` and `if-expr2` are `defined`. |
51+
| `!if-expr1` | `defined` if `if-expr1` is not `defined`. |
52+
| `( if-expr1 )` | defined if `if-expr1` is defined. |
53+
| `symbol` | `defined` if it is flagged as defined by the `-define` compiler option. |
54+
55+
The logical operators have the usual logical precedence.
56+
57+
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.
58+
59+
Conditional compilation directives can be nested. Indentation is not significant for preprocessor directives.
60+
5561
## NULLABLE directive
5662

5763
Starting with F# 9, you can enable nullable reference types in the project:
@@ -84,6 +90,8 @@ When you use the `#line` directive, file names must be enclosed in quotation mar
8490

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

93+
Note that `#line` directives do not influence the behavior of `#nowarn` / `#warnon`. These two directives always relate the the file that is being compiled.
94+
8795
## See also
8896

8997
- [F# Language Reference](index.md)

0 commit comments

Comments
 (0)