http://d.puremagic.com/issues/show_bug.cgi?id=1594
Summary: version not honored for invarient declaration
Product: D
Version: 1.022
Platform: PC
OS/Version: Windows
Status: NEW
Severity: normal
Priority: P2
Component: DMD
AssignedTo: bugzilla digitalmars.com
ReportedBy: jemandy earthlink.net
The following program does not compile in 1.022. The error message is
"invar.d(6): statement expected to be { }, not int".
import std.stdio;
version (D_Version2)
{
string str = "Version 2";
invariant int myint = 2;
}
else
{
string str = "Version 1";
const int myint = 1;
}
void main()
{
writefln( "str: %s\nmyint: %s", str, myint );
return;
}
Notice that a version 1 compiler shouldn't be trying to compile the statement
where the error occurs: it's in a block designated as being for a different
version of the compiler. Change the single occurrence of if "invariant" to
"const", and it compiles fine. With this change, the output of the program is
str: Version 1
myint: 1
just as you would hope.
--

http://d.puremagic.com/issues/show_bug.cgi?id=1594
------- Comment #1 from ddparnell bigpond.com 2007-10-19 01:53 -------
I know its not intuitive but the compiler is behaving correctly. All the code
inside a version block must be valid code for the compiler you are using.
Here is how you need to do what you want to achieve...
version(D_Version2)
{
string str = "Version 2";
mixin("invariant int myint = 2;");
}
else
{
string str = "Version 1";
const int myint = 1;
}
--

I know its not intuitive but the compiler is behaving correctly. All the code
inside a version block must be valid code for the compiler you are using.
Here is how you need to do what you want to achieve...
version(D_Version2)
{
string str = "Version 2";
mixin("invariant int myint = 2;");
}
else
{
string str = "Version 1";
const int myint = 1;
}

Hmmmm. The following would also work:
version (D_Version2)
{
mixin( "alias invariant int invarInt;" );
invarInt myint = 2;
}
Unfortunately, this does not work:
version (D_Version2)
{
mixin( "alias invariant int invarInt;" );
}
else
{
alias const int invarInt;
}
Neither v.1 nor v. 2 likes alias of const. (I can't figure out why neither
likes alias of const but v. 2 likes alias of invariant. const is a storage
class, not a type modifier; but then, I presume, the same is true for
invariant in v. 2.)
Could an invariant storage class be added to version 1 that is just equivalent
10 const? We obviously don't want to add a new storage class semantics to
version 1--changes to v. 1 at this point should be limited essentially to bug
fixes. However, using the word invariant with const semantics might be a small
enough change to be a worthwhile. It would be a convenience to those trying to
develop for version 1 and version 2 sinultaneously.

I know its not intuitive but the compiler is behaving correctly. All the code
inside a version block must be valid code for the compiler you are using.
Here is how you need to do what you want to achieve...
version(D_Version2)
{
string str = "Version 2";
mixin("invariant int myint = 2;");
}
else
{
string str = "Version 1";
const int myint = 1;
}

Hmmmm. The following would also work:
version (D_Version2)
{
mixin( "alias invariant int invarInt;" );
invarInt myint = 2;
}
Unfortunately, this does not work:
version (D_Version2)
{
mixin( "alias invariant int invarInt;" );
}
else
{
alias const int invarInt;
}
Neither v.1 nor v. 2 likes alias of const. (I can't figure out why neither
likes alias of const but v. 2 likes alias of invariant. const is a storage
class, not a type modifier; but then, I presume, the same is true for
invariant in v. 2.)
Could an invariant storage class be added to version 1 that is just equivalent
10 const? We obviously don't want to add a new storage class semantics to
version 1--changes to v. 1 at this point should be limited essentially to bug
fixes. However, using the word invariant with const semantics might be a small
enough change to be a worthwhile. It would be a convenience to those trying to
develop for version 1 and version 2 sinultaneously.

And make the type constructor flavors no ops in v1 (invariant(char)[]
--> char[]), and make .idup synonymous with .dup. These things would go
a long way to making it easier to support both v1 and v2 of D I think.