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
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.
d-bugmail@puremagic.com escribi
(In reply to comment #2) > 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.
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.
(In reply to comment #4) > 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.
Fixed DMD 1.018 and DMD 2.002
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル