[Issue 1228] New: Class invariants should not be called before the object is fully constructed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri May 11 11:04:30 PDT 2007


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

           Summary: Class invariants should not be called before the object
                    is fully constructed
           Product: D
           Version: 1.014
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: onlystupidspamhere at yahoo.se


The spec should state whether invariants hold only after the constructor has
finished or not. http://www.webber-labs.com/research/paste01.ps contains some
discussion about class invariants in Java, and might be helpful. 

The following code shows how D currently checks invariants even before the
class has been fully constructed. IMO invariants should hold only after the
constructor has finished and there is a valid reference to the object.

Code:

import tango.io.Stdout;

class foo {

        this() {
                Stdout("entering constructor").newline;
                bar();
                Stdout("leaving constructor").newline;
        }

        void bar() {
                Stdout("method bar()").newline;
        }

        invariant {
                Stdout("invariant").newline;
        }
}

void main() {
        auto a = new foo();
}

Output:

entering constructor
invariant
method bar()
invariant
leaving constructor
invariant


-- 



More information about the Digitalmars-d-bugs mailing list