Thread overview | |||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 11, 2014 Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
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
--
Using Opera's revolutionary email client: http://www.opera.com/mail/
|
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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 See this pull request[1] and the linked enhancement report. Also note that calling `r.popFront()` without checking `r.empty` is a program error (so it's recommended to at least put in an assert). [1] https://github.com/D-Programming-Language/phobos/pull/1866 |
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | On Tue, 11 Feb 2014 10:10:27 -0000, Regan Heath <regan@netmail.co.nz> 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. Further, the naive solution of adding .array gets you in all sorts of trouble :p (The whole byLine buffer re-use issue). This should be simple and easy, dare I say it trivial.. or am I just being dense here. R -- Using Opera's revolutionary email client: http://www.opera.com/mail/ |
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | > Further, the naive solution of adding .array gets you in all sorts of trouble :p (The whole byLine buffer re-use issue).
>
> This should be simple and easy, dare I say it trivial.. or am I just being dense here.
>
> R
The second naive solution would be to use readText and splitLines.
|
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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 |
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On Tue, 11 Feb 2014 10:52:39 -0000, Tobias Pankrath <tobias@pankrath.net> wrote: >> Further, the naive solution of adding .array gets you in all sorts of trouble :p (The whole byLine buffer re-use issue). >> >> This should be simple and easy, dare I say it trivial.. or am I just being dense here. >> >> R > > The second naive solution would be to use readText and splitLines. The file is huge in my case :) R -- Using Opera's revolutionary email client: http://www.opera.com/mail/ |
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On Tue, 11 Feb 2014 10:58:17 -0000, Tobias Pankrath <tobias@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/ |
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | On Tuesday, 11 February 2014 at 13:00:19 UTC, Regan Heath wrote: > 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 byLine does not use opApply https://github.com/D-Programming-Language/phobos/blob/master/std/stdio.d#L1389 |
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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 foreach (i, line; iota(size_t.max).zip(range.take(4))) { } |
February 11, 2014 Re: Ranges, constantly frustrating | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | On 02/11/2014 06:25 AM, Rene Zwanenburg wrote: > On Tuesday, 11 February 2014 at 10:10:27 UTC, Regan Heath wrote: >> foreach (i, line; range.take(4)) //Error: cannot infer argument types >> { >> ..etc.. >> } > foreach (i, line; iota(size_t.max).zip(range.take(4))) > { > > } There is also the following, relying on tuples' automatic expansion in foreach: foreach (i, element; zip(sequence!"n", range.take(4))) { // ... } Ali |
Copyright © 1999-2021 by the D Language Foundation