version(number) is completely useless

Walter Bright newshound2 at digitalmars.com
Thu Jul 21 04:11:57 UTC 2022


On 7/19/2022 3:30 PM, Hipreme wrote:
> version(V1_3)
>      version = V1_2;
> version(V1_2)
>      version = V1_1;
> version(V1_1)
>      version = V1_0;

This kind of thing is indeed terrible. Fortunately, there are better ways.

Let's say you've got FeatureX, and it exists in some versions and not in others.

Worse way:

     version (X) doX();

Better way:

     import X;
     doX();

Inside module X:

     module X;

     version (X)
         void X() { ... }
     else
         void X() {}

Now, let's say version Y and Z both rely on X being implemented:

     import X;
     doX();

module X:

     version (Y) import Ximpl : X;
     else version (Z) import Ximpl : Y;
     else void X() { }

module Ximpl:

     module Ximpl;

     void X() { ... }

Essentially, what you wind up with is, for each of the versions, there will be a 
straightforward list of which features are implemented for it. These manifests 
only appear once in your code, and are easy to maintain.

The complaint about this comes from the DRY principle, people do not want to 
duplicate a single line of code (in this case, the import lines). But once one 
gets past DRY, the readability of each version having its own list begins to 
look much more attractive.

Even better, when one adds a new version, the addition becomes straightforward 
rather than weaving it into a tangle of other conditional compilation sections.

I know you're not convinced. But I ask that you think about it, and give it a 
try. I bet you like the results. And you won't have that conditional compilation 
mess *per file* anymore.


More information about the Digitalmars-d mailing list