digitalmars.D.learn - opSlice and foreach with ranges
- =?ISO-8859-1?Q?Ali_=C7ehreli?= (61/61) Feb 17 2011 Is that not implemented yet?
- Jesse Phillips (2/8) Feb 17 2011 Yeah I don't think it is implemented, file a bug.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (4/12) Feb 17 2011 http://d.puremagic.com/issues/show_bug.cgi?id=5605
Is that not implemented yet? TDPL mentions a very useful feature on page 381 under "12.9.1 foreach with Iteration Primitives". (Note: I am copying all of this manually; the typos are mine): It first shows a function that includes a possible "compiler rewrite" of a foreach loop: void process(SimpleList!int lst) { for (auto __c = lst; !__c.empty; __c.popFront()) { auto value = __c.front; ... // Use value of type int } } It then says <quote> ... if the iterated object offers the slice operator with no arguments lst[], __c is initialized with lst[] instead of lst. This is in order to allow "extracting" the iteration means out of a container without requiring the container to define the three iteration primitives. </quote> I couldn't get that to work with the following code: import std.stdio; struct MyRange { int theOnlyOne; property bool empty() const { return false; } property ref int front() { return theOnlyOne; } void popFront() {} } struct MyCollection { MyRange opSlice() const { return MyRange(); } } void main() { auto coll = MyCollection(); foreach (i; coll) { // <-- compilation error // ... } } Error: cannot infer type for i Providing the type of i as 'int' or 'ref int' produce a different error: foreach (int i; coll) { or foreach (ref int i; coll) { produce Error: no property 'opApply' for type 'MyCollection' Please note that taking a slice explicitly works: foreach (i; coll[]) { but not the feature mentioned in TDPL. Thank you, Ali
Feb 17 2011
Ali Çehreli Wrote:<quote> ... if the iterated object offers the slice operator with no arguments lst[], __c is initialized with lst[] instead of lst. This is in order to allow "extracting" the iteration means out of a container without requiring the container to define the three iteration primitives. </quote>Yeah I don't think it is implemented, file a bug.
Feb 17 2011
On 02/17/2011 01:49 PM, Jesse Phillips wrote:Ali �ehreli Wrote:http://d.puremagic.com/issues/show_bug.cgi?id=5605 Thank you, Ali<quote> ... if the iterated object offers the slice operator with no arguments lst[], __c is initialized with lst[] instead of lst. This is in order to allow "extracting" the iteration means out of a container without requiring the container to define the three iteration primitives. </quote>Yeah I don't think it is implemented, file a bug.
Feb 17 2011