October 22, 2004 keyword: invariant | ||||
---|---|---|---|---|
| ||||
According to the documentation only classes might contain "invariant". Dmd-0.104 allows "invariant" in structs and checks those contracts. test case: svn://svn.kuehne.cn/dstress/nocompile/invariant_04.d Dmd-0.104 allows "invariant" in unions and checks those contracts. test case: svn://svn.kuehne.cn/dstress/nocompile/invariant_06.d The documentation doesn't forbid the usage of a class function named "_invariant". The function will not be handled as a "invariant" contract, that's correct. The problem is that it's mangled name will conflict with "invariant". test case: svn://svn.kuehne.cn/dstress/run/invariant_11.d Code: # class Parent{ # int x; # this(){ # x=3; # } # # invariant{ # assert(x>2); # } # } # # class Child : Parent{ # int y; # this(){ # y=5; // [2] # } # # invariant{ # assert(y>4); // [1] # } # } # # int main(){ # Child gc = new Child(); # return 0; # } The Child's invariant[1] will be called before "y=5" [2] is executed. It seems that not only inherited the Child it's Parent's invariant but so did the Parent with the Child's invariant. I think this is a bug and not a hole in the documentation. Reason: This way contracts for new class members can't easily be added to a sub-class. test cases: svn://svn.kuehne.cn/dstress/run/invariant_1[45].d Thomas |
October 23, 2004 Re: keyword: invariant | ||||
---|---|---|---|---|
| ||||
Posted in reply to Thomas Kuehne | in addition: the invariant of a class that has got no own constructor is _not_ called after the super constructor finished. test case: svn://svn.kuehne.cn/dstress/run/invariant_18.d Thomas |
Copyright © 1999-2021 by the D Language Foundation