Bug 1312 – invariant storage class within a struct/class
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2007-07-03T20:50:00Z
Last change time
2015-06-09T01:04:59Z
Keywords
accepts-invalid, wrong-code
Assigned to
nobody
Creator
Daniel919
Comments
Comment #0 by Daniel919 — 2007-07-03T20:50:43Z
There is an unwanted behaviour when using an invariant storage class within a struct/class.
------------------------------------------------------------------------
import std.stdio;
struct MyStruct
{
int x = 1;
invariant int y = 2;
//void resety() { y = 3; }; //error: constant y is not an lvalue - as expected
/*
alternatives, that work as expected:
const int y = 2;
static invariant int y = 2;
final int y = 2; //but this takes up storage for each instance
*/
}
void main() {
MyStruct mystruct;
mystruct.x = 1; //ok
//mystruct.y == 1, expected: 2
//mystruct.y = 2; //no error, but mystruct.x also becomes 2
writefln("mystruct.sizeof: %s mystruct.x: %s mystruct.y: %s", mystruct.sizeof, mystruct.x, mystruct.y);
}