fslang-suggestions icon indicating copy to clipboard operation
fslang-suggestions copied to clipboard

Add preprocessor directives #define and #undef

Open sangkilc opened this issue 3 years ago • 2 comments

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:

  • [x] This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • [x] I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • [x] This is not a breaking change to the F# language design
  • [ ] I or my company would be willing to help implement and/or test this

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.

sangkilc avatar Mar 24 '22 19:03 sangkilc

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?

njlr avatar May 27 '23 15:05 njlr

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.

jwosty avatar Jul 06 '23 15:07 jwosty