'version'-based code selection

Anonymouse zorael at gmail.com
Sun Jun 2 17:43:09 UTC 2019


On Saturday, 1 June 2019 at 07:46:40 UTC, Jonathan M Davis wrote:
> For the most part though, you don't declare your own version 
> identifiers. It sometimes makes sense, but usually, version 
> identifiers are used for versioning code based on the platform 
> or architecture that it's compiled on. They're really only 
> intended to be a saner version of #ifdefs, and if you're doing 
> anything fancy with them, you're really not using them as 
> intended and are probably going to have problems.

I use versioning pervasively to make features opt-in/opt-out at 
compile-time.

Like so, from dub.json:

     "versions":
     [
         "AsAnApplication",
         "WithAdminPlugin",
         "WithAutomodePlugin",
         "WithBashQuotesPlugin",
         "WithChanQueriesService",
         "WithChatbotPlugin",
         "WithConnectService",
         "WithCTCPService",
         "WithHelpPlugin",
         "WithNotesPlugin",
         "WithPersistenceService",
         "WithPipelinePlugin",
         "WithPrinterPlugin",
         "WithQuotesPlugin",
         "WithSedReplacePlugin",
         "WithSeenPlugin",
         "WithWebtitlesPlugin"
     ],

---

module foo;

version(WithFoo):

// ...

Is this recommended against? It's a very convenient way of 
enabling and disabling modules outright, since (by default) dub 
eagerly compiles everything it sees. I haven't had any problems 
with it as of yet, at the very least.



More information about the Digitalmars-d-learn mailing list