Comment #0 by bearophile_hugs — 2013-06-10T14:38:25Z
struct Foo {
int x = 0;
invariant() {
assert(x != 0);
}
void bar() {}
}
void main() {
Foo f;
f.bar;
invariant int y; // deprecation warning
}
With dmd 2.064alpha this code gives the deprecation warning:
temp.d(11): Deprecation: use of 'invariant' rather than 'immutable' is deprecated
And then correctly asserts at run-time:
core.exception.AssertError@temp(4): Assertion failure
- - - - - - - - - - - - - - - - - - -
The first step (I suggest in dmd 2.064) is to disallow 'invariant' as a replacement for the 'immutable' turning the deprecation into an error, and at the same time allow the definition of class/struct invariants without the ( ), and and at the same time a give a deprecation message where a ( ) is used (the alternative is to give just a warning, but this change has a low probability of introducing bugs in D code, so a deprecation is enough):
struct Foo {
int x = 0;
invariant() { // deprecation warning here, no () needed.
assert(x != 0);
}
invariant { // OK
assert(x != 0);
}
void bar() {}
}
void main() {
Foo f;
f.bar;
invariant int y; // error
}
- - - - - - - - - - - - - - - - - - -
In a successive compiler release the usage of ( ) at the struct/class invariant becomes an error to tidy up the language:
struct Foo {
int x = 0;
invariant() { // error
assert(x != 0);
}
invariant { // OK
assert(x != 0);
}
void bar() {}
}
void main() {
Foo f;
f.bar;
invariant int y; // error
}
Comment #6 by bearophile_hugs — 2014-05-12T07:38:24Z
I'd like to reopen this issue because it's not done yet. The last missing step is to (in future) give an error in line 2 of program, disallowing the () after invariant:
struct Foo {
invariant() {
assert(false);
}
void bar() {}
}
unittest() {} // Not allowed.
void main() {
Foo f;
f.bar;
}
Comment #7 by bearophile_hugs — 2014-05-12T09:48:19Z
Reopened. Waiting for opinions.
Comment #8 by temtaime — 2014-05-12T09:49:42Z
I agree () should give deprecation for now and error in future.
Comment #9 by k.hara.pg — 2014-05-17T10:23:11Z
(In reply to bearophile_hugs from comment #6)
> I'd like to reopen this issue because it's not done yet. The last missing
> step is to (in future) give an error in line 2 of program, disallowing the
> () after invariant:
>
>
> struct Foo {
> invariant() {
> assert(false);
> }
> void bar() {}
> }
> unittest() {} // Not allowed.
> void main() {
> Foo f;
> f.bar;
> }
Instead of that, how about to accept both invariant(){...} and unittest(){...} ?
Comment #10 by temtaime — 2014-05-17T10:26:21Z
@Kenji it's useless imo.
Comment #11 by k.hara.pg — 2014-05-17T10:39:51Z
(In reply to Temtaime from comment #10)
> @Kenji it's useless imo.
My main concern is, that disallowing current invariant() syntax will break many existing D2 code. I'm afraid to decrease language stability.
On the other hand, accepting the syntax `unittest(){...}` would not have any design benefit, but it will increase syntax consistency.
Comment #12 by temtaime — 2014-05-17T10:41:49Z
I think we should simply show deprecate message for invariant() to help updating user's code and remove it in future.
It's best way imo.
Comment #13 by bearophile_hugs — 2014-05-17T12:17:26Z
(In reply to Kenji Hara from comment #9)
> Instead of that, how about to accept both invariant(){...} and
> unittest(){...} ?
I prefer to disallow () in both.
Comment #14 by yebblies — 2014-05-17T16:24:28Z
(In reply to Kenji Hara from comment #9)
>
> Instead of that, how about to accept both invariant(){...} and
> unittest(){...} ?
I think that's a good idea. I see very little benefit to enforcing one syntax over the other. There doesn't seem much potential for confusion in having two syntaxes if both unittest and invariant work the same way.
Comment #15 by briancschott — 2014-05-18T00:09:06Z
(In reply to bearophile_hugs from comment #13)
> (In reply to Kenji Hara from comment #9)
>
> > Instead of that, how about to accept both invariant(){...} and
> > unittest(){...} ?
>
> I prefer to disallow () in both.
I vote for this also. The parenthesis serve no purpose. The compiler should accept () with a deprecation message and then we should get rid of them.
Quoting from bug 5038:
"Well, this is a very strange situation. invariant without parentheses is LEGAL IN D1! When converting D1 code to D2, you get this nonsense deprecation message asking you to use immutable instead.
We have existing production code which uses invariant without parentheses!
We should make it legal again. This would be an undeprecation, which AFAIK has never happened before in D."
Comment #16 by robert.schadek — 2024-12-13T18:08:04Z