You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
I found that Contract.ForAll enumerates it's argument when perform checking post- or pre- conditions. Such behavior could produce unexpected multiple enumeration in some cases where it isn't desirable. For example:
enumerable has no ending (infinite sequence)
generator has side effects
generator is resource eager
I propose to generate substitution for parameters/arguments that checks conditions lazily.
publicIEnumerable<int>Generator(){
Contract.Requires(Contract.ForAll(Contract.Result<IEnumerable<int>>(),x =>x>0))
while (...)yield return ...;}
This code should be rewritten to validate once the values will be produced.
Let's say we have captured the result into result of type IEnumerable<int> from code above. Then instead of enumerating it, need to replace with result.Select(x => { contract_check(x > 0); return x; }).
Same thing could be done with preconditions.
There's still an option to check by enumerating when the argument is array or List (IList doesn't work, because it also could be generated).
The text was updated successfully, but these errors were encountered:
I found that
Contract.ForAll
enumerates it's argument when perform checking post- or pre- conditions. Such behavior could produce unexpected multiple enumeration in some cases where it isn't desirable. For example:I propose to generate substitution for parameters/arguments that checks conditions lazily.
This code should be rewritten to validate once the values will be produced.
Let's say we have captured the result into
result
of typeIEnumerable<int>
from code above. Then instead of enumerating it, need to replace withresult.Select(x => { contract_check(x > 0); return x; })
.Same thing could be done with preconditions.
There's still an option to check by enumerating when the argument is array or
List
(IList
doesn't work, because it also could be generated).The text was updated successfully, but these errors were encountered: