version and debug statements
Sean Kelly
sean at f4.ca
Thu May 11 10:32:20 PDT 2006
Walter Bright wrote:
> Anders F Björklund wrote:
>> I'm still naively hoping for both of "version (Unix)"
>
> DMD supports Windows and Linux; Unix is neither. For a compiler which
> targets the Unix operating system, it should set the Unix version.
For what it's worth, I think it would be useful for the 'Posix' version
to be added, so any system supporting POSIX would have version 'Posix'
automatically specified in addition to any OS version identifier. This
would be similar to how Windows platforms also have either 'Win32' or
'Win64' defined. While a good bit of POSIX declarations are indeed
implementation dependent, an equally large amount are not, and I believe
it would be useful for a version identifier to reflect this.
>> and "version (!Windows)" to be defined and legal in D.
>
> In C and C++, I often see:
>
> #if !WIN32
>
> when what is really meant is:
>
> #if linux
>
> i.e. the former is almost always a bug waiting to happen (when someone
> tries to compile for a third operating system). Versions should be "this
> build is for this configuration" rather than "this build is not for that
> configuration."
As much as I like the version idea, I'm beginning to feel that the C/C++
#ifdef method may actually be preferable in some situations. For
example, some portions of Posix are common and others are not, so I am
faced with a few options:
- Define a separate set of Posix headers for each OS and have the user
import the proper set manually.
- Define a separate set of Posix headers for each OS and do some fancy
versioning in a common area to publicly import the proper set automatically.
- Define a common set of modules, each of which contains version blocks
for each OS and may potentially result in multiple declarations of the
same symbol occuring in the file (this is what I'm doing now for the
Ares Posix headers as it's the most readable, but I think it may become
difficult to deal with when support for more OSes is added)
- Define a common set of modules with centralized logic for determining
various things and use 'static if' in place of 'version' in a manner
similar to #ifdef in C/C++
- Define a common set of modules but specify version identifiers in the
makefile or via other means and move the complicated logic out of code
and into a configure script or something similar
While preprocessor logic has proven to be an aboslute nightmare in terms
of readability and maintainability in some cases, I truly believe that
this is more attributable to a lack of programmer skill than anything
else. And while I love that D encourages 'good' style in many cases,
I'm still undecided whether the current version scheme will prove to be
sufficiently robust for large cross-platform projects. Currently, I
think the last option may be the best compromise (and would require no
change to the language spec), but I'm still not certain whether it will
prove more readable to do version checking outside of code as opposed to
inside.
> One can also write:
> version (ThisFeature)
> {
> }
> else
> {
> version = ThatFeature;
> }
>
> ...
>
> version (ThatFeature)
> ...
>
> For an example of why D is this way, see the sources to the Hans Boehm
> garbage collector, where the complex thicket of #if's makes it extremely
> difficult to see exactly what is being compiled.
Yup, but doing this in every module isn't particularly desirable if such
settings may be common for an entire package. I'll admit I'm not
entirely sure what the best approach is in this case. I mentioned the
options above mostly in hopes that doing so would help me think through
the ideas a bit.
Sean
More information about the Digitalmars-d
mailing list