0

Is it possible in D to define a variable inside a contract? I have the following function as member of an interface:

public @safe nothrow void eat(in ulong chunklength)
in { assert(chunklength < length); ulong oldlength = length; } // lenght is a member variable
out { assert(length == oldlength - chunklength); }
Jeroen
  • 15,257
  • 12
  • 59
  • 102
  • 1
    [This discussion](http://forum.dlang.org/thread/mailman.164.1369528727.13711.digitalmars-d@puremagic.com) at the dlang forums suggests that it isn't supported (unless that has changed in the last year). – Michael May 18 '14 at 18:21

2 Answers2

1

This is not possible in D. Your only option is to make oldLength a member variable or static class variable, or a global variable. While variables are accessible across different unittest blocks (though this is an implementation detail and not guaranteed), that's not the case for contracts.

Meta
  • 1,091
  • 6
  • 14
-1

Based on C language scoping rules, I'd have assumed that declaring "oldlength" inside the {} for "in" would make it local to that scope and not visible in the scope active when for the out piece of the contract. Would moving the declaration outside the contract help?