Cross module version specs

Steven Schveighoffer schveiguy at yahoo.com
Thu Apr 26 05:37:44 PDT 2012


On Thu, 26 Apr 2012 06:32:58 -0400, James Miller <james at aatch.net> wrote:

> On Thursday, 26 April 2012 at 10:20:37 UTC, Jonathan M Davis wrote:
>> On Thursday, April 26, 2012 12:09:19 James Miller wrote:
>>> which pretty much makes them completely useless unless
>>> you only use the built-in versions.
>>
>> That's not true at all. It just means that versions are either useful  
>> for
>> something within a module or they're intended for your program as a  
>> whole and
>> passed on the command line (e.g. StdDdoc is used by Phobos, and it's not
>> standard at all; the makefile adds it to the list of compiler flags).  
>> But yes,
>> it's true that if you want to define a version in one module which  
>> affects
>> another, you can't do it.
>
> Is there any reason for that limitation? Seems like an arbitrary limit  
> to me.
>
> The library I am binding to uses ifdefs to let the compiler see the  
> appropriate declarations in the header files. It would be nice in  
> general for D to be able to mimic that capability, as it means you can  
> have a "configuration" file with a list of specs that can be generated  
> at build-time by something like autoconf.

No, it would not be nice, it would be horrible.  C's preprocessor is one  
of the main reasons I sought out something like D.  The fact that you can  
include files in a different order and get a completely different result  
is not conducive to understanding code or keeping code sane.

The correct thing to use for something like this is enums and static ifs.   
They work because enums are fully qualified within the module they are  
defined, and you can't define and use the same unqualified enums in  
multiple places.

I'll give you some examples:

module1.d:
version = abc;

module2.d:
version(abc) int x;
else  double x;

module3.d:
import module1;
import module2;

pragma(msg, typeof(x).stringof); // int or double?

module4.d:
import module3;

version(abc) int y;
else double y;

pragma(msg, typeof(y).stringof); // int or double?


Now, what happens?  Should module2 be affected by module1's versions?   
What about module4?  What if module4 doesn't know that module3 indirectly  
declares abc as a version?  What if module3 didn't import module1 at the  
time module4 was written, but added it later?

These kinds of decoupled effects are what kills me when I ever read a  
heavily #ifdef'd header file.

versions should be defined for the *entire program*, not just for certain  
files.  And if they are defined just for certain files, define them in the  
file itself.

-Steve


More information about the Digitalmars-d mailing list