fsharp / fslang-suggestions

The place to make suggestions, discuss and vote on F# language and core library features
344 stars 21 forks source link

Add preprocessor directives #define and #undef #1124

Open sangkilc opened 2 years ago

sangkilc commented 2 years ago

I propose we add #define and #undef preprocessor directives for conditional compilation (as in C#: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#defining-symbols).

Pros and Cons

The advantages of making this adjustment to F# are

  1. Good for quick debugging and code maintenance
  2. No ground-breaking change to the language

Honestly, I don't know if there's any disadvantage.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: I was not able to find relevant issues.

Affidavit (please submit!)

Please tick this by placing a cross in the box:

Please tick all that apply:

For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

njlr commented 1 year ago

This would make it possible to write include guards (like in C/C++), which would prevent multiple defines when using scripts.

A.fsx

#if !A_FSX
#define A_FSX

let a = 123

#endif

B.fsx

#load "A.fsx"

C.fsx

#load "A.fsx"
#load "B.fsx"

Although I would prefer if there were an idempotent variant of #load - perhaps based on hashing?

jwosty commented 1 year ago

I run into this, in particular when using #if directives in library code which I want to test in FSI, (most recently checking against FABLE_COMPILER). #define would be useful even if it were an FSI-only feature.