Out contracts: how to refer to objects' start state

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sat May 25 17:43:37 PDT 2013


On 5/25/13 8:38 PM, Peter Williams wrote:
> For some class methods, to express comprehensive out{} contracts it is
> necessary to be able to refer to the state of the class object before
> the operation as well as after it e.g. if the method adds something to a
> container you need to be able to specify that nothing was accidentally
> deleted from the container during the method. I've scoured the language
> specification and Andrei's book for advice on how to do this without any
> luck.
>
> Can it be done and, if so, how?
>
> Thanks
> Peter

Unfortunately we don't have a solution to that. A while ago I proposed 
that the "in" and "out" contracts share the same scope. That would allow 
us to do:

class A {
   void fun()
   in { auto oldLen = this.length; }
   out { assert(this.length == oldLen + 1); }
   body { ... }
}

That was technically difficult to do back then, and fell by the wayside. 
Today it would break too much code to introduce even if feasible.


Andrei


More information about the Digitalmars-d mailing list