Bug 1594 – version not honored for invarient declaration
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2007-10-19T00:19:00Z
Last change time
2014-02-24T15:33:18Z
Assigned to
bugzilla
Creator
jemandy
Comments
Comment #0 by jemandy — 2007-10-19T00:19:15Z
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.
Comment #1 by ddparnell — 2007-10-19T01:53:10Z
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;
}
Comment #2 by jemandy — 2007-10-19T14:10:25Z
[email protected] Wrote:
> 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.
Comment #3 by jemandy — 2007-11-01T18:11:47Z
Oops, I think I was supposed to close this since I was the one that opened it in the first place.