Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Enumerable lazy checks #19

Open
Igorbek opened this issue Mar 12, 2015 · 0 comments
Open

Enumerable lazy checks #19

Igorbek opened this issue Mar 12, 2015 · 0 comments

Comments

@Igorbek
Copy link

Igorbek commented Mar 12, 2015

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.

public IEnumerable<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).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant