[Issue 10326] New: Disallow 'invariant' for immutable, allow class/struct invariants without (), and later disallow usage of ()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 10 14:38:26 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=10326

           Summary: Disallow 'invariant' for immutable, allow class/struct
                    invariants without (), and later disallow usage of ()
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2013-06-10 14:38:25 PDT ---
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 at 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
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list