logoalt Hacker News

diathyesterday at 5:49 PM2 repliesview on HN

    out (; balance == balance + amount) // checked after method returns
How exactly does it work? Is this a typo?

Replies

prydtyesterday at 6:01 PM

Looks like its a typo :(

The correct way to go about this would be to return the new balance and capture the return value in the first part of the out postcondition like:

```D double deposit(double amount) in (amount > 0, "Deposit amount must be positive") out (result; result == balance) { balance += amount; return balance; } ```

My mistake!

https://dlang.org/spec/function.html#postconditions

show 1 reply
lgasyesterday at 5:58 PM

I've never used D, but it appears to be valid syntax. https://dlang.org/spec/function.html#postconditions

show 2 replies