syntax question on "invariant" keyword

Michael Kiermaier michael.kiermaier at
Tue Jul 3 15:00:46 PDT 2007

I tried an example from

*** begin Test.d ***
import std.stdio;

struct S
    int x;
    invariant int y;

void main() {
    writefln(S.sizeof); // prints 4, not 8
*** end Test.d ***

Compilation gives the error message
Test.d(5): statement expected to be { }, not int

So I changed the line
    invariant int y;
    invariant {int y;}
and now the compilation works.

Why do I need the curly braces here?
And why does the example not work? Was there a change to the syntax rules of "invariant"?

Tanks in advance,


More information about the Digitalmars-d-learn mailing list