www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5605] New: foreach with ranges doesn't support opSlice()

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5605

           Summary: foreach with ranges doesn't support opSlice()
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: acehreli yahoo.com


--- Comment #0 from Ali Cehreli <acehreli yahoo.com> 2011-02-17 14:19:37 PST ---
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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 17 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5605


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|foreach with ranges doesn't |[tdpl] foreach with ranges
                   |support opSlice()           |doesn't support opSlice()


--- Comment #1 from Steven Schveighoffer <schveiguy yahoo.com> 2011-02-18
04:20:34 PST ---
Adding [tdpl] for better searching

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 18 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5605


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch, rejects-valid
           Platform|Other                       |All
         OS/Version|Linux                       |All


--- Comment #3 from Kenji Hara <k.hara.pg gmail.com> 2011-12-25 01:07:30 PST ---
https://github.com/D-Programming-Language/dmd/pull/579

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 25 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5605


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


--- Comment #4 from Walter Bright <bugzilla digitalmars.com> 2011-12-25
15:16:31 PST ---
https://github.com/D-Programming-Language/dmd/commit/4443f4295c1ee4b33794e6e2b3d050b1075a3599

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 25 2011