DMD Bug or not? foreach over struct range

Era Scarecrow rtcvb32 at yahoo.com
Wed May 16 00:48:20 PDT 2012


On Wednesday, 16 May 2012 at 07:41:14 UTC, Jonathan M Davis wrote:
> void main()
> {
>     prime_walk pw;
>     pw.cap = 100;
>     foreach(i; pw)
>         writefln("%s %s", i, pw.position);
> }
>
> You'd see that pw.postion is always printed as 2, just like 
> Nick's foo.val
> always prints as 0.

  Hmmm...
[quote]
void main()
{
    Foo foo;
    foreach(val; foo)
        writeln(foo.val, " ", val);
}
[/quote]

  Indeed...  he is using foo.val isn't he? Instead he should just 
use 'val' by itself.



  Quick overview, my little prime walker is dropping the 
requirement of checking for if a number is prime by only going up 
and being on valid prime numbers. Each time the old prime is used 
it's stepped up in the array (and can't step on another used 
element), this removes your 'is this a prime' down to basically 
O(1). You can't ask randomly is x a prime, but if you needed a 
range of primes you can get quite a large list very quickly. Half 
of it is there for optimization purposes.


More information about the Digitalmars-d-learn mailing list