Bug 8786 – assert does not call "invariant()" function

Status
RESOLVED
Resolution
FIXED
Severity
trivial
Priority
P2
Component
dlang.org
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-09T01:19:00Z
Last change time
2015-06-09T05:14:54Z
Assigned to
nobody
Creator
monarchdodra

Comments

Comment #0 by monarchdodra — 2012-10-09T01:19:41Z
According to http://dlang.org/class.html#Invariant "The invariant can be checked when a class object is the argument to an assert() expression, as: " //---- Date mydate; ... assert(mydate); // check that class Date invariant holds //---- But I get: //---- struct S { invariant(){} } void main() { S s; assert(s); } //---- Error: expression s of type S does not have a boolean value //---- IMO, behavior of assert should be: 1. Check for argument can be cast to bool. If yes, do it. The call should trigger invariant check anyways. 2. Else, call invariant directly.
Comment #1 by bearophile_hugs — 2012-10-09T01:44:22Z
This was discussed a lot...
Comment #2 by issues.dlang — 2012-10-09T01:52:20Z
Yes, _class_ object's have their invariant called. You're using a struct. It's cast to bool (like it would be in the condition of an if statement), and your struct doesn't define a cast to bool, so it doesn't work. If you want to check the invariant, then take its address so that you're operating on a pointer to a struct. In that case, it will act the same way that a class does.
Comment #3 by monarchdodra — 2012-10-09T03:24:37Z
(In reply to comment #2) > Yes, _class_ object's have their invariant called. You're using a struct. It's > cast to bool (like it would be in the condition of an if statement), and your > struct doesn't define a cast to bool, so it doesn't work. > > If you want to check the invariant, then take its address so that you're > operating on a pointer to a struct. In that case, it will act the same way that > a class does. Thanks for the explanation. I was not present when "this was discussed a lot", and the documentation didn't really make it clear. Anyhow, I took the liberty of updating the documentation. https://github.com/D-Programming-Language/d-programming-language.org/pulls Changing to trivial/website/assigned.
Comment #4 by issues.dlang — 2012-10-09T10:50:32Z
The behavior of classes was discussed a lot (in particular, it didn't use to check whether the reference was null first, which caused a lot of problems). I'm not sure that the behavior of structs has been discussed much or that it is necessarily entirely desirable (it's certainly not well-documented), but that's how it works right now. Certainly, it makes perfect sense that the struct would be converted to bool. Whether it should also have its invariant called is debatable, but as long as the conversion to bool involves calling a public member function, the invariant will be called anyway. So, the behavior is probably fine. The fact that the invariant is explicitly checked with a pointer to a struct is then essentially the same as what happens with classes, so really, the way that structs currently work with assert is probably the best way to go about it. The main problem is that it's not necessarily intuitive and that it's not properly documented. So, if the docs are updated, then it should be fine.
Comment #5 by monarchdodra — 2012-10-15T08:08:36Z
Documentation updated.