Remix.run Logo
diath 2 days ago

    out (; balance == balance + amount) // checked after method returns
How exactly does it work? Is this a typo?
prydt 2 days ago | parent | next [-]

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

Jtsummers 2 days ago | parent [-]

HN does not use markdown code block formatting so this is hard to read. Formatting code blocks is simple, two blank spaces before any line to make it a code block and no need for extra newlines (unlike between paragraphs):

  double deposit(double amount)
    in (amount > 0, "Deposit amount must be positive")
    out (result; result == balance)
  {
    balance += amount;
    return balance;
  }
lgas 2 days ago | parent | prev [-]

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

diath 2 days ago | parent | next [-]

I'm not asking about the syntax, I'm asking about the logic where a value can be equal to itself plus another value when the pre-condition is that it must be > 0.

prydt 2 days ago | parent | prev [-]

The syntax is correct but I made a logical error since balance is being compared to itself (as opposed to the new balance at the end).