syntax question on "invariant" keyword

Derek Parnell derek at nomail.afraid.org
Tue Jul 3 16:14:34 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

This seems to work ...

//-----------------
import std.stdio;
alias invariant int iint;  // To help remove the stupid ambiguity
                           // caused by excessively overloaded keywords
struct S
{
    int x;
    iint y;
}
void main() {
    writefln(S.sizeof); // prints 4, not 8
}
//-----------------

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


More information about the Digitalmars-d-learn mailing list