D foreach

Discussion in 'Python' started by bearophileHUGS@lycos.com, Nov 13, 2005.

  1. Guest

    The Digital Mars D compiler is a kind of "improved c++", it contains a
    "foreach" statement:
    http://www.digitalmars.com/d/statement.html#foreach

    Usage example:
    foreach(int i, inout int p; v1) p = i;

    Is equal to Python:
    for i in xrange(len(v)): v = i

    That is: v1 = range(len(v1))
    (Some people use something like this in Python to scan a list of lists,
    so p become a reference to a list, that can be modified in place, but
    it's not much explicit way of doing things.)


    Another example:
    foreach(int i, int p; v2) v1 = p;

    Is equal to Python:
    for i,p in enumerate(v2): v1 = p

    So the variable p contains (scans) the elements of the given iterable
    object, but if you assign p with a value, that value becomes copied
    inside the mutable iterable too. Those are little examples, but I think
    it can be quite useful in more complex code.

    Bye,
    bearophile
    , Nov 13, 2005
    #1
    1. Advertising

  2. wrote:
    > The Digital Mars D compiler is a kind of "improved c++", it contains a
    > "foreach" statement:
    > http://www.digitalmars.com/d/statement.html#foreach
    >
    > Usage example:
    > foreach(int i, inout int p; v1) p = i;
    >
    > Is equal to Python:
    > for i in xrange(len(v)): v = i

    [...]
    > So the variable p contains (scans) the elements of the given iterable
    > object, but if you assign p with a value, that value becomes copied
    > inside the mutable iterable too. Those are little examples, but I think
    > it can be quite useful in more complex code.


    1. It would be difficult to implement. Python would require the concept
    of 'reference to variable', which has lots of repercussions for
    reference counting, garbage collection etc. Requiring iterators to
    return references would also break all existing iterators. It would also
    be required that the assignment operator is overloadable and this is
    another box of Pandora no one likes to open (well, no one except C++
    programmemrs).

    Or the compiler would have to detect 'assignment to iterator variable'
    and issue an 'update_current' to the iterator.

    2. It violates the Zen of Python 'Explicit is better than implicit'
    (although the definition of 'explict' varies wildly in the Python community)

    3. For specific collections like lists and dictionaries, you could write
    a wrapper so that it is possible to write

    for ref in wrapper (mycollection):
    print ref.value
    ref.value = newvalue

    Daniel
    Daniel Dittmar, Nov 14, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin Spencer

    Re: foreach loop error

    Kevin Spencer, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    359
    Kevin Spencer
    Aug 22, 2003
  2. Luc Kumps

    Re: foreach loop error

    Luc Kumps, Aug 22, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    375
    Luc Kumps
    Aug 22, 2003
  3. =?Utf-8?B?SnVzdGlu?=

    Problem with foreach loop

    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    321
    Hans Kesting
    Oct 22, 2004
  4. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    739
    Scott Allen
    Feb 12, 2005
  5. caldera
    Replies:
    1
    Views:
    4,402
    Scott Allen
    Feb 22, 2005
Loading...

Share This Page