Design by Contract - out old struct member value

Nick Treleaven nick at geany.org
Wed Aug 6 16:19:17 UTC 2025


On Wednesday, 6 August 2025 at 15:43:01 UTC, Brother Bill wrote:
> How is this done in D?

I thought the following would work, but the compiler doesn't like 
`this.count` as a default argument:

```d
struct Counter {
    int count;

    void incrementBy(int increment, int preCount = this.count)
    in {
       assert(increment >= 0, "increment must be zero or greater");
    }
    out {
        assert(count == preCount + increment);
    }
    do {
       this.count += increment;
    }
}
```

And I expect you'd like to avoid adding a function parameter 
anyway.


More information about the Digitalmars-d-learn mailing list