I suggest we ...

Compiler directive for environment variables

It would be nice to have a compiler directive for environment variables which can be used in string literals.
This way we can pass different parameters to type providers on a build server.

[<Literal>]
let ConnectionString = #env "MY_DB_CONNECTIONSTRING"
let cmd = new SqlCommandProvider<"...", ConnectionString>()

4 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Lenne shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    2 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...
      • Gauthier Segay commented  ·   ·  Flag as inappropriate

        Alexey, your example would cause "This is not a valid constant expression." error.

        Lenne, do you mean the value to be substituted at compile time? (I guess yes as it is a compiler directive).

        I'm not sure I see value in this, is there prior art (any other compiler does that?)

        I generally move those type of strings in a separate module, and can consider generating this module file as a preliminary build step.

        Also, I think this can be addressed by a type provider altogether, maybe you should consider this instead?

      • Alexei Odeychuk commented  ·   ·  Flag as inappropriate

        Lenne, what's wrong with already-existing .NET built-in Environment.GetEnvironmentVariable method (from namespace System)?

        You can use a .NET built-in method instead of the #env syntax suggested. The name of the built-in method would make your code more readable.

        Code example:

        open System

        [<Literal>]
        let ConnectionString = Environment.GetEnvironmentVariable("MY_DB_CONNECTIONSTRING")

        let cmd = new SqlCommandProvider<"...", ConnectionString>()

        Please see for more details:
        Environment.GetEnvironmentVariable Method (String)
        https://msdn.microsoft.com/en-us/library/77zkk0b6(v=vs.110).aspx

        Hope it helps.

      F# Language

      Feedback and Knowledge Base