Thread overview
[Issue 3194] New: invariant should be checked at the beginning and end of protected functions
Jun 11, 2011
yebblies
Jun 22, 2011
Diego Canuhe
Oct 09, 2011
Walter Bright
July 20, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3194

           Summary: invariant should be checked at the beginning and end
                    of protected functions
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrei@metalanguage.com


Consider:

class A
{
    invariant() { ... }
    public void f() { ... }
    protected void g() { ... }
}

Currently invariant is called at the beginning and end of each public function, the end of the constructor, and the beginning of the destructor. Scott Meyers pointed out to a quite known fact - protected is much closer to public than to private in terms of offering access control. This is because anyone can just inherit from a class and call protected methods, or even wrap them in public methods.

Consequently, it looks like the invariant of a class must also hold upon entry and exit of all protected methods.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 11, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3194


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |yebblies@gmail.com


--- Comment #1 from yebblies <yebblies@gmail.com> 2011-06-11 12:10:59 PDT ---
Patch for D2 https://github.com/D-Programming-Language/dmd/pull/112

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 22, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3194


Diego Canuhe <canuhedc@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |canuhedc@gmail.com


--- Comment #2 from Diego Canuhe <canuhedc@gmail.com> 2011-06-22 11:52:38 PDT ---
(In reply to comment #1)
> Patch for D2 https://github.com/D-Programming-Language/dmd/pull/112

I disagree.
Even while you can easily access protected methods of a class, they are
intended for internal use, otherwise they'd be public.

Consider:

class A
{
    abstract protected SomeType[] getData();

    public void processData()
    {
        //do something that breaks the invariants
        foreach (SomeType element; getData())
        {
            //do something...
        }
        //do something that restores the invariants
    }

    invariant() {...}
}

a class B derived from A implementing getData() shouldn't have to respect the
invariants while entering/leaving getData(). Anything regarding getData()'s
side-effects should be put in an out clause.

BTW, abstract functions shouldn't require a body clause accompanying in and out clauses

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3194


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2011-10-09 12:54:14 PDT ---
https://github.com/D-Programming-Language/dmd/commit/95c4a647d224962e6323cecf5ddff961ac38da99

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