www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1228] New: Class invariants should not be called before the object is fully constructed

reply d-bugmail puremagic.com writes:
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 digitalmars.com
        ReportedBy: onlystupidspamhere 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


-- 
May 11 2007
next sibling parent reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1228






Invariants are checked after every public member function's execution as well,
so the behavior you're seeing at least meets the spec.  Now, I can see some
arguments for not having invariants checked until construction is complete, but
that'd be a change in documented behavior.


-- 
May 11 2007
parent Ary Manzana <ary esperanto.org.ar> writes:
d-bugmail puremagic.com escribió:
 http://d.puremagic.com/issues/show_bug.cgi?id=1228
 
 
 
 
 

 Invariants are checked after every public member function's execution as well,
 so the behavior you're seeing at least meets the spec.  Now, I can see some
 arguments for not having invariants checked until construction is complete, but
 that'd be a change in documented behavior.
 
 
Sound reasonable to me, too. You can always refactor to: class foo { this() { Stdout("entering constructor").newline; barInternal(); Stdout("leaving constructor").newline; } void bar() { barInternal(); } private void barInternal() { Stdout("method bar()").newline; } invariant { Stdout("invariant").newline; } }
May 11 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1228







 Sound reasonable to me, too. You can always refactor to:
 
 class foo {
 
          this() {
                  Stdout("entering constructor").newline;
                  barInternal();
                  Stdout("leaving constructor").newline;
          }
 
          void bar() {
                  barInternal();
          }
 
         private void barInternal() {
                 Stdout("method bar()").newline;
         }
 
          invariant {
                  Stdout("invariant").newline;
          }
 }
This slows down the release build since at least DMD doesn't inline member functions. Yes, this is a design decision and I'm not sure which one is better. --
May 12 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1228






Since the invariant is called at the start of public or
exported members, such members should not be called from
constructors. I'll update the documentation to indicate this.


-- 
Jun 30 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1228







 Since the invariant is called at the start of public or
 exported members, such members should not be called from
 constructors. I'll update the documentation to indicate this.
Good, updating the docs works for me. --
Jul 01 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=1228


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED





Fixed DMD 1.018 and DMD 2.002


-- 
Jul 01 2007