out contract misunderstanding (was: I cannot understand problem with argument of the function)

monarch_dodra monarchdodra at gmail.com
Thu Sep 19 00:45:37 PDT 2013


On Thursday, 19 September 2013 at 06:39:09 UTC, Ivan Kazmenko 
wrote:
> On Thursday, 19 September 2013 at 01:41:15 UTC, mrd wrote:
>> $ ./bug
>> body, value=2
>> out contract, value=0
>>
>> Why argument "value" in contract isn't equal 2 ?
>
> Why should it be?  value is a mutable argument, and the out 
> contract evaluates it at the return point, which is after it's 
> set to 0.  It is no different than using value in the body just 
> before the return point.
>
> If you want to preserve the original arguments until the return 
> point, use const and create a mutable duplicate.
>
> Ivan Kazmenko.

I actually asked about this on the main threads:

http://forum.dlang.org/thread/gdmwjyrefhreghznbibg@forum.dlang.org

I actually disagree though: How the function is implemented 
should have no bearing on how the output contract should be 
implemented. Both should get their own copy of the args. The 
implementation of a function should not have to create a local 
duplicate just because it happens to know that it has an out 
contract.

This is particularly relent since (in theory), an in/out contract 
should appear in a function's interface, and compiled/called by 
client code (though that's not the case today).


More information about the Digitalmars-d-learn mailing list