D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1312 - invariant storage class within a struct/class
Summary: invariant storage class within a struct/class
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid, wrong-code
Depends on:
Blocks:
 
Reported: 2007-07-03 20:50 UTC by Daniel
Modified: 2015-06-09 01:04 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Daniel 2007-07-03 20:50:43 UTC
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);
}
Comment 1 Don 2009-09-10 14:16:16 UTC
This was fixed in DMD2.022 or earlier.