Ranges, constantly frustrating

Regan Heath regan at netmail.co.nz
Tue Feb 11 05:00:19 PST 2014


On Tue, 11 Feb 2014 10:58:17 -0000, Tobias Pankrath <tobias at pankrath.net>  
wrote:

> On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote:
>> Things like this should "just work"..
>>
>> File input ...
>>
>> auto range = input.byLine();
>> while(!range.empty)
>> {
>>   range.popFront();
>>   foreach (i, line; range.take(4))  //Error: cannot infer argument types
>>   {
>>     ..etc..
>>   }
>>   range.popFront();
>> }
>>
>> Tried adding 'int' and 'char[]' or 'auto' .. no dice.
>>
>> Can someone explain why this fails, and if this is a permanent or  
>> temporary limitation of D/MD.
>>
>> R
> Is foreach(i, val; aggregate) even defined if aggr is not an array or  
> associated array? It is not in the docs:  
> http://dlang.org/statement#ForeachStatement

import std.stdio;

struct S1 {
    private int[] elements = [9,8,7];
    int opApply (int delegate (ref uint, ref int) block) {
        foreach (uint i, int n ; this.elements)
            block(i, n);
        return 0;
    }
}

void main()
{
	S1 range;	
	foreach(uint i, int x; range)
	{
	  writefln("%d is %d", i, x);
	}
}

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list