www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - DIP proposal: Require in-contract expressions to be a superset of the

DIP proposal is here: 
https://github.com/FeepingCreature/DIPs/blob/DIP-inclusive-in-contracts/DIPs/1NNN-MB.md

Preview PR is here: https://github.com/dlang/dmd/pull/11465

Previous discussion is here: 
https://forum.dlang.org/thread/mjcppoaykiwqegkfrerb forum.dlang.org

Summary in code:

class Parent {
   void foo(int i) in (i >= 0) { }
}

class Child : Parent {
   // Why does this say i >= 5? I don't know, maybe the parent 
contract used to be i >= 5.
   // Maybe it's a typo. The point is:
   void foo(int i) in (i >= 5) {
     int value = array[i - 5]; // RangeError.
     // Why? The actual in-contract for `foo` is
     // `in (i >= 0 /* from parent */ || i >= 5)`.
     // IOW `in (i >= 0)`. Our `in` does nothing.
     // This is bad and should be changed.
   }
}

Proposed change: if the child method's in-contract is tighter 
than the parent, ie. when the child in-contract as written fails 
when the parent passes (which is not allowed to happen in 
Liskov), then raise a runtime error.
Jul 27 2020