Thread overview
Re: syntax question on "invariant" keyword
Jul 03, 2007
Michael Kiermaier
Jul 03, 2007
BCS
Jul 03, 2007
Daniel919
Jul 04, 2007
Michael Kiermaier
Jul 04, 2007
Frits van Bommel
July 03, 2007
BCS Wrote:

> This is getting into the ambiguity between an invariant function (that is part of design by contract)

Thanks, I didn't know that "invariant" can also be used for functions;

> and an invariant value (that is one form of const).
> 
> I think the way that it should be done is
> 
> invariant(int) y;

I tried that, but:
Test.d(6): found 'int' when expecting ')'
Test.d(6): statement expected to be { }, not )
Test.d(6): found ')' instead of statement

Also, http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=8217 claims that "invariant int" and "invariant(int)" are NOT the same, but "invariant int" and  "final invariant(int)" are the same.

I don't know if that is true.
July 03, 2007
Reply to Michael,

> BCS Wrote:
> 
>> This is getting into the ambiguity between an invariant function
>> (that is part of design by contract)
>> 
> Thanks, I didn't know that "invariant" can also be used for functions;
> 
>> and an invariant value (that is one form of const).
>> 
>> I think the way that it should be done is
>> 
>> invariant(int) y;
>> 
> I tried that, but:
> Test.d(6): found 'int' when expecting ')'
> Test.d(6): statement expected to be { }, not )
> Test.d(6): found ')' instead of statement
> Also,
> http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmar
> s.D.learn&article_id=8217 claims that "invariant int" and
> "invariant(int)" are NOT the same, but "invariant int" and  "final
> invariant(int)" are the same.
> 
> I don't know if that is true.
> 

Are you using dmd 2.x or 1.x?


July 03, 2007
Are you using dmd 1.x ? The final-const-invariant thing only works on the experimental 2.x version. I tried your example there and it compiles without errors as expected.

> Also, http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=8217 claims that "invariant int" and "invariant(int)" are NOT the same, but "invariant int" and  "final invariant(int)" are the same.
> 
> I don't know if that is true.

"invariant(int) x" this is useless and does not mean that x can't be changed, what someone might think at first.
Using brackets means that you can assign another instance of TYPE to the variable. But you can't change the data of the instance.

"invariant(int) x" means: you can assign another instance of int to x
You see, this doesn't make sense.
It's equal useless for all simple storage classes.

However, "final invariant(int) x = 1" works like "invariant int x = 1",
because final means: you can't assign another int to x
July 04, 2007
Daniel919 Wrote:

> Are you using dmd 1.x ? The final-const-invariant thing only works on the experimental 2.x version. I tried your example there and it compiles without errors as expected.

Well, that seems to be the problem:

My dmd command reports:
Digital Mars D Compiler v1.018

Where can I get the 2.x version? I cannot find it in the "download" section on the Digitalmars D hompage

And what exactly is the status of 2.x? I believe I read somewhere, that D 2.0 was officially released. But now you write that it is still experimental.

> "invariant(int) x" this is useless and does not mean that x can't be
> changed, what someone might think at first.
> Using brackets means that you can assign another instance of TYPE to the
> variable. But you can't change the data of the instance.
> 
> "invariant(int) x" means: you can assign another instance of int to x
> You see, this doesn't make sense.
> It's equal useless for all simple storage classes.
> 
> However, "final invariant(int) x = 1" works like "invariant int x = 1", because final means: you can't assign another int to x

Thanks for the explanation!

~michael
July 04, 2007
Michael Kiermaier wrote:
> Daniel919 Wrote:
> 
>> Are you using dmd 1.x ? The final-const-invariant thing only works on the experimental 2.x version. I tried your example there and it compiles without errors as expected.
> 
> Well, that seems to be the problem:
> 
> My dmd command reports:
> Digital Mars D Compiler v1.018
> 
> Where can I get the 2.x version? I cannot find it in the "download" section on the Digitalmars D hompage

It's on the changelog page: http://www.digitalmars.com/d/changelog.html
Be sure to click the 2.002 link, the "latest version" link seems to be out of date (points to 2.001).

> And what exactly is the status of 2.x? I believe I read somewhere, that D 2.0 was officially released. But now you write that it is still experimental.

It's an experimental alpha version. That means it's got the latest additions, but may be more buggy and later versions can change and break code that used to work.