Out contracts: how to refer to objects' start state

Peter Williams pwil3058 at bigpond.net.au
Sat May 25 18:54:15 PDT 2013


On 26/05/13 10:43, Andrei Alexandrescu wrote:
> 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.

That looks like a good solution to me.

> Today it would break too much code to introduce even if feasible.

How would it break code?  I've tried to imagine how but have failed.

Peter


More information about the Digitalmars-d mailing list