dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.82k stars 773 forks source link

Preprocessor Directives: #elif missing #17282

Closed Thorium closed 3 weeks ago

Thorium commented 3 weeks ago

F# seems to have #elif preprocessor directive / pragma missing: https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/compiler-directives#preprocessor-directives

C# has it: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#conditional-compilation

I know it's not good practice to use lot of these, I'm not trying to do a templating engine, I would just like to do a simple switch like this:

[<Literal>]
let myPath =
#if WIN64
  "/library/x64/runtime.dll"
#elif WIN86
  "/library/x86/runtime.dll"
#elif MAC
  "/library/iOS/runtime-osx.dll"
#else
  "/library/unix/runtime.dll"
#endif

The current workaround:

[<Literal>]
let myPath =
#if WIN64
  "/library/x64/runtime.dll"
#endif 
#if WIN86
  "/library/x86/runtime.dll"
#endif 
#if MAC
  "/library/iOS/runtime-osx.dll"
#endif 
#if !WIN64 && !WIN86 && !MAC
  "/library/unix/runtime.dll"
#endif
abelbraaksma commented 3 weeks ago

I think this is a reasonable suggestion, can you copy this over to https://github.com/fsharp/fslang-suggestions? It is where suggestions go and can be upvoted. It is used for suggestions that require a language, core lib or syntax change. If accepted by Don, we will then proceed with an RFC and, hopefully, an implementation.

Note that there's some prior discussion here: https://github.com/fsharp/fslang-suggestions/issues/273 (if you look at the archived comments, you can see that that was still from the F# 4 days in Codeplex. The design of the time is gone, but clearly, the part that says #elif didn't make it into the language at the time).


edit, note that #if can be nested, so your code example may be easier if turned into this:

let myPath =
#if WIN64
    "/library/x64/runtime.dll"
#else
    #if WIN86
        "/library/x86/runtime.dll"
    #else
        #if MAC
            "/library/iOS/runtime-osx.dll"
        #else
            "/library/unix/runtime.dll"
        #endif
    #endif
#endif

(I mean, it would be easier to read with your suggestion, but at least you won't need the complex boolean expressions)

abelbraaksma commented 3 weeks ago

Closing this, as it is now here: https://github.com/fsharp/fslang-suggestions/issues/1370

KevinRansom commented 2 weeks ago

elif is an excellent suggestion.