[Issue 6857] Precondition contract checks should be statically bound.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Mar 2 01:57:29 PST 2012


http://d.puremagic.com/issues/show_bug.cgi?id=6857


Don <clugdbug at yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug at yahoo.com.au


--- Comment #4 from Don <clugdbug at yahoo.com.au> 2012-03-02 01:57:16 PST ---
(In reply to comment #3)
> (In reply to comment #2)
> > (In reply to comment #1)
> > > Can you elaborate why the static type must be considered? What's the problem
> > > with using the dynamic type?
> > 
> > The problem is simple. Let's consider a class A and a subclass B.
> > 
> > Then a function as this :
> > 
> > void fun(A a) {
> >     a.foo();
> > }
> > 
> > If passed an element of type B, fun will work, because B is a subclass of A. If
> > B.foo's contract is different than A.foo's, then B.foo's in contract is
> > executed.
> > 
> > It is a bug, because fun has no knowledge weither it manipulate an element of
> > type A or an element of type B. It means that fun must respect the in contract
> > provided by A.foo, because in other case, it may or may no work, depending on
> > how fun in called, which isn't a reliable behavior.
> 
> Yesterday I was sleepy and wasn't able to understand it. But even now I fail to
> see the issue.
> First fun accepts any instance that is of class A or a subclass of A. And
> independent of this whenever you call a.foo() all in contracts must be checked
> using based on a's dynamic type.

The point is that fun() must satisfy the precondition for A. The fact that it's
actually receiving a subclass of A is irrelevant.
Since subclasses of A can only widen the precondition, they don't need to be
checked. (Since they must pass A.in(), by definition they will pass A.in() ||
B.in()).

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


More information about the Digitalmars-d-bugs mailing list