Testing lazy ranges in post-conditions

Peter Alexander via Digitalmars-d digitalmars-d at puremagic.com
Mon Nov 24 06:03:50 PST 2014


On Monday, 24 November 2014 at 12:20:40 UTC, bearophile wrote:
> Peter Alexander:
>
>> Should be able to do:
>>
>> assert(result.save.all!(x => x < 10));
>>
>> But iota's save isn't const, so you can't (that's a bug).
>
> Mine was just an example of the general problem, another 
> example:
>
>
> import std.range, std.algorithm;
>
> auto foo()
> out(result) {
>     assert(result.all!(b => b.length == 2));
> } body {
>     auto a = new int[10];
>     return a.chunks(2);
> }
>
> void main() {}

Chunks.save should also be const, so result.save.{...} should 
work.

It becomes a real problem with input ranges, because you can't 
save them. That makes sense though, as there is no way to consume 
the result in a post-condition check that doesn't consume it. 
That's just a fact of life and a limitation of trying to verify 
mutable data.



More information about the Digitalmars-d mailing list