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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun May 6 00:34:54 PDT 2012


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



--- Comment #56 from Don <clugdbug at yahoo.com.au> 2012-05-06 00:36:04 PDT ---
(In reply to comment #55)
> Mea culpa.
> 
> I read Meyer's book again. Chapter 16.1 "Cutting out the middleman" pg. 575
> says:
> 
> "A client of MATRIX must satisfy the original (stronger) precondition, and may
> only expect the original (weaker) postcondition; even if its request gets
> served dynamically by NEW_MATRIX it has no way of benefiting from the broader
> tolerance of inputs and tighter precision of results. To get this improved
> specification it must declare the matric to be of type NEW_MATRIX, thereby
> losing access to other implementations represented by descendants of MATRIX
> that are not also descendants of NEW_MATRIX."
> 
> (MATRIX is the base class, NEW_MATRIX is the derived class.)
> 
> So I'm reopening it as a normal bug.
> 
> Unfortunately, I do not currently see a reasonable way of implementing this.
> Fortunately, as is it does not inhibit correct programs, it only accepts some
> invalid ones.

I think implementation is simpler than what happens at present. You need a
static function thunk for each virtual function with a precondition.
For example:

class A {
  int foo(int n) in { assert(n >0); } body { return n; }
}
class B {
  int foo(int n) { return n+1; } // no change to precondition
}
class C {
  int foo(int n) in { assert(n > -10 && n<-6); } body { return n+2; }

becomes (I've put 'virtual' in to make things clearer):

class A {
 static void foo_in(A x) {  assert(n>0); }
 virtual int foo(int n) { foo_in(this, n);  return foo_body(n); }
 virtual int foo_body(int n) { return n; }
}

class B {
 virtual int foo_body(int n) { return n+1; }
}

class C {
  static void foo_in(C x, int n) { assert(n > -10 && n<-6); }
  virtual int foo(int n ) {
    if (C.foo_in(this, n)) return foo_body(n); // passed C's precondition
   return A.foo(n); // failed, try A's precondition
 }
  virtual int foo_body(int n) { return n+2; }
}

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