Search code examples
dcontract

Variable declaration in contracts


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); }

Solution

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