www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8786] New: assert does not call "invariant()" function

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8786

           Summary: assert does not call "invariant()" function
           Product: D
           Version: unspecified
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: monarchdodra gmail.com



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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 09 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8786


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



This was discussed a lot...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 09 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8786


Jonathan M Davis <jmdavisProg gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com



PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 09 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8786


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
          Component|DMD                         |websites
           Severity|major                       |trivial




 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. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 09 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8786




PDT ---
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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 09 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8786


monarchdodra gmail.com changed:

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



Documentation updated.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 15 2012