www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 9437] New: unwanted behavior from phobos range

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

           Summary: unwanted behavior from phobos range
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bioinfornatics gmail.com



04:57:46 PST ---
Dear,

Recently i seen that the range used into tje loop is not the same instance as
this one created. the followed code show the problem


-------------------------------------------------
import std.stdio;

struct R{
    private:
        size_t _position;
        string _word;

    public:
        this( string word ){
            _word = word;
        }

         property size_t length () const {
            return _word.length;
        }

         property bool empty() const{
            return ( _position >= _word.length );
        }

        char front() const {
            return _word[ _position ];
        }

        void popFront(){
            _position++;
        }

        ubyte toUbyte() const {
            return cast(ubyte) _word[_position] ;
        }

        alias length opDollar;

}

void main( string[] args ){
    R r = R( "Hello D users" );
    foreach( l; r )
        writeln( l, ", ", r.front() );
}
-------------------------------------------------


his output:
-------------------------------------------------
 ./tesRange 
H, H
e, H
l, H
l, H
o, H
 , H
D, H
 , H
u, H
s, H
e, H
r, H
s, H
-------------------------------------------------

The problem:
- it seem is do a copy so when you create a range over a mmfile it will mapped
the file more than once while when we use mmfile we want some perf
- we are not able to implement some method to give the current state wile is
looping. For this you should return all possible value that you could be used
in popFront() !!!

If someone know how to querry the instance used to loop i am aware

thank

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


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |monarchdodra gmail.com



This works as intended, and is the documented behavior of foreach.

foreach is supposed to be used on the elements only. You cannot add, remove, or
interface with the range in general during a foreach. If you need a more
specific behavior, just use a for:

void main( string[] args ){
    R r = R( "Hello D users" );
    for( ; !r.empty ; r.popFront() )
    {
        auto l = r.front;
        writeln( l, ", ", r.front() );
    }
}

Please see the thread in regards to the mm file thing.

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




06:25:56 PST ---
Ok, but silently doing this that is really bad and a is really a thing in D
where could create many bug

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





 Ok, but silently doing this that is really bad and a is really a thing in D
 where could create many bug
But silently doing what? If the range provides random access, then which behavior would you expect? //---- int arr[] = [1, 2, 3] foreach ( a ; arr) writeln(arr.front); //So this print 1, 2, 3? assert(arr.length == 3); //But this should pass? //---- The expected behavior of foreach is *only* to iterate on each element of the range, but it is also expected to *not* modify the range. It is modifying the range that would provide surprising results. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 01 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=9437




07:16:24 PST ---
i do not talk to modify a range while is in a loop but access to const method
should be possible.
<type> name() const { ... }

what is silently ?


    R r = R( "Hello D users" );
    foreach( l; r )
        writeln( l, ", ", r.front() );

you have 2 variable r and are not the same for me is dangerous. I know now what
happen.
But when you look this code it seem ok but is not.

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





 i do not talk to modify a range while is in a loop but access to const method
 should be possible.
 <type> name() const { ... }
 
 what is silently ?
 
 
     R r = R( "Hello D users" );
     foreach( l; r )
         writeln( l, ", ", r.front() );
 
 you have 2 variable r and are not the same for me is dangerous. I know now what
 happen.
 But when you look this code it seem ok but is not.
But that's the point, in that code, both "r" ARE the same variable. r is r. It is the *iteration* that is done on an un-named copy. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 01 2013