[Home]
[Search]
[D]
DebugSpecification DebugAttribute DebugStatement VersionSpecification VersionAttribute VersionStatement StaticAssert
It is inevitable that the D language will evolve over time. Therefore, the version identifier namespace beginning with "D_" is reserved for identifiers indicating D language specification or new feature conformance.
Furthermore, predefined version identifiers from this list cannot be set from the command line or from version statements. (This prevents things like both Windows and linux being simultaneously set.)
Compiler vendor specific versions can be predefined if the trademarked vendor identifier prefixes it, as in:
version(DigitalMars_funky_extension)
{
...
}
It is important to use the right version identifier for the right
purpose. For example, use the vendor identifier when using a vendor
specific feature. Use the operating system identifier when using
an operating system specific feature, etc.
DebugSpecification debug = Identifier ; debug = Integer ; VersionSpecification version = Identifier ; version = Integer ;Version specifications do not declare any symbols, but instead set a version in the same manner that the -version does on the command line. The version specification is used for conditional compilation with version attributes and version statements.
VersionSpecifications and DebugSpecifications apply only to the module they appear in. The only global ones are the predefined ones and any that are specified on the command line.
The version specification makes it straightforward to group a set of features under one major version, for example:
version (ProfessionalEdition)
{
version = FeatureA;
version = FeatureB;
version = FeatureC;
}
version (HomeEdition)
{
version = FeatureA;
}
...
version (FeatureB)
{
... implement Feature B ...
}
DebugStatement: DebugPredicate Statement DebugPredicate Statement else Statement DebugPredicate debug Statement debug ( Integer ) debug ( Identifier )Debug statements are compiled in when the -debug switch is thrown on the compiler.
debug(Integer) statements are compiled in when the debug level is >= Integer.
debug(Identifier) statements are compiled in when the debug identifier matches Identifier.
If Statement is a block statement, it does not introduce a new scope. For example:
int k;
debug
{ int i;
int k; // error, k already defined
i = 3;
}
x = i; // uses the i declared above
VersionStatement: VersionPredicate Statement VersionPredicate Statement else Statement VersionPredicate version ( Integer ) version ( Identifier )The version statement conditionally compiles in its statement body based on the version specified by the Integer of Identifier. Both forms are set by the -version switch to the compiler. If Statement is a block statement, it does not introduce a new scope. For example:
int k;
version (Demo) // compile in this code block for the demo version
{ int i;
int k; // error, k already defined
i = 3;
}
x = i; // uses the i declared above
The version statement works together with the version attribute
for declarations.
Version statements can nest.
The optional else clause gets conditionally compiled in if the version predicate is false:
version (X86)
{
... // implement custom inline assembler version
}
else
{
... // use default, but slow, version
}
While the debug and version statements superficially behave the same,
they are intended for very different purposes. Debug statements
are for adding debug code that is removed for the release version.
Version statements are to aid in portability and multiple release
versions.
DebugAttribute: debug debug ( Integer ) debug ( Identifier )Two versions of programs are commonly built, a release build and a debug build. The debug build includes extra error checking code, test harnesses, pretty-printing code, etc. The debug attribute conditionally compiles in code:
class Foo
{
int a, b;
debug:
int flag;
}
Conditional Compilation means that if the code is not
compiled in, it still must be syntactically correct, but no
semantic checking or processing is done on it. No symbols are
defined, no typechecking is done, no code is generated, no
imports are imported.
Various different debug builds can be built with a parameter to debug:
debug(Integer) { } // add in debug code if debug level is >= Integer
debug(identifier) { } // add in debug code if debug keyword is identifier
These are presumably set by the command line as
-debug=n and -debug=identifier.
VersionAttribute: version ( Integer ) version ( Identifier )The version attribute is very similar to the debug attribute, and in many ways is functionally interchangeable with it. The purpose of it, however, is different. While debug is for building debugging versions of a program, version is for using the same source to build multiple release versions.
For instance, there may be a full version as opposed to a demo version:
class Foo
{
int a, b;
version(full)
{
int extrafunctionality()
{
...
return 1; // extra functionality is supported
}
}
else // demo
{
int extrafunctionality()
{
return 0; // extra functionality is not supported
}
}
}
Various different version builds can be built with a parameter
to version:
version(n) { } // add in version code if version level is >= n
version(identifier) { } // add in version code if version keyword is identifier
These are presumably set by the command line as
-version=n and -version=identifier.
StaticAssert: static assert ( Expression );Expression is evaluated at compile time, and converted to a boolean value. If the value is true, the static assert is ignored. If the value is false, an error diagnostic is issued and the compile fails.
Unlike AssertExpressions, StaticAsserts are always checked and evaluted by the compiler unless they appear in a false debug or version conditional.
void foo()
{
if (0)
{
assert(0); // never trips
static assert(0); // always trips
}
version (BAR)
{
static assert(0); // does not trip unless BAR is defined
}
}