strange behavior of by-value function arguments in postcondition

bauss jj_1337 at live.dk
Tue Aug 31 11:48:01 UTC 2021


On Tuesday, 31 August 2021 at 11:40:42 UTC, Mike Parker wrote:
> On Tuesday, 31 August 2021 at 10:43:41 UTC, Andrzej K. wrote:
>
>> I would intuitively assume that the contract of such a 
>> function is: "I will not modify your objects, and I will 
>> select a number from the range that your objects indicate."
>
> For the first half of that assumption to be valid, you would 
> need this function signature:
>
> ```d
> int select(const int lo, const int hi) {}
> ```

And holds true because this statement is then disallowed:

```d
   lo = hi; // why not
```

Which means "lo" can never be modified or rather never will be 
modified. At least in this context considering it's a value-type. 
If it was a reference type then obviously it could still change 
as external sources can modify it and only immutable would 
guarantee that it stays the same.


More information about the Digitalmars-d mailing list