syntax question on "invariant" keyword

Derek Parnell derek at nomail.afraid.org
Tue Jul 3 16:42:12 PDT 2007


On Tue, 03 Jul 2007 18:00:46 -0400, Michael Kiermaier wrote:

> I tried an example from
> http://www.digitalmars.com/d/final-const-invariant.html:
> 
> *** 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;
> into
>     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,
> 
> ~michael

I get that fact that invariant struct members don't take up space in the
struct (though I don't think that is a smart move) but I don't understand
the results of this code below ...

// ------------
import std.stdio;

alias invariant int iint;

struct S
{
    int x = 9;
    invariant int y = 8;
    void foo()
    {
        // y  = 7; // Expectedly, this fails to compile (GOOD).
    }
}
void main() {
    S a;
    writefln("Size of S is %s", S.sizeof);
    writefln("Before x=%s y=%s", a.x, a.y);
    a.x = 1;
    a.y = 2;
    writefln("After  x=%s y=%s", a.x, a.y);
}
// ------------
The results I get using DMD 2.002 are ...

c:\temp>test
Size of S is 4
Before x=9 y=9
After  x=2 y=2

-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
4/07/2007 9:39:54 AM


More information about the Digitalmars-d-learn mailing list