Invariants are not allowed to call public functions _directly_.(Issue 520)
However, they can do indirectly, and that makes an infinite loop.
Code:
//import std.stdio; // for trace log
class Inv {
invariant() {
//writeln("invariant");
prv();
//pub(); // not allowed
}
private void prv() const {
//writeln("private");
pub();
}
void pub() const {
//writeln("public");
}
}
void main() {
auto test = new Inv();
test.pub();
}
Comment #1 by maxim — 2013-01-27T22:22:16Z
From http://dlang.org/dbc.html:
"The code in the invariant may not call any public non-static members of the class or struct, either directly or indirectly. Doing so will result in a stack overflow, as the invariant will wind up being called in an infinitely recursive manner. "
I understand that it does not mean that implementation enforces the rules. Ability to call functions from invariants is a loophole, and there is possibility to enter infinite loop in cases which are more complicated then above. Human still can write code which avoids compiler constraints - the question here is whether to introduce control flow for calling functions from invariants, or throw exceptions, or just do nothing.
Either this should be marked as enhancement request, or RESOLVED-WONTFIX
Comment #2 by kekeniro2 — 2013-10-12T01:05:04Z
*** This issue has been marked as a duplicate of issue 10889 ***