473,387 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

D foreach

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] = 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[i] = p;

Is equal to Python:
for i,p in enumerate(v2): v1[i] = 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 22 '05 #1
1 3706
be************@lycos.com 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] = 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
Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Phil | last post by:
Hi, I read somewhere that the new version (v1.1) has improved the performance of 'foreach' over 'for'. Is that true? I did some measurements and I still think for has an upperhand... ? Phil
32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
8
by: cody | last post by:
currently, foreach takes a IEnumerable as parameter, so we can do: foreach (int i in array){} what about an additional form of foreach that takes an IEnumerator as parameter, so we can do: ...
104
by: cody | last post by:
What about an enhancement of foreach loops which allows a syntax like that: foeach(int i in 1..10) { } // forward foeach(int i in 99..2) { } // backwards foeach(char c in 'a'..'z') { } // chars...
3
by: cody | last post by:
why foreach does always have to declare a new variable? I have to write foreach (int n in array){} but Iam not allowed to write: int n=0; foreach (n in array){}
13
by: TrintCSD | last post by:
How can I reset the collections within a foreach to be read as a change from within the foreach loop then restart the foreach after collections has been changed? foreach(string invoice in...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
3
by: Wiktor Zychla [C# MVP] | last post by:
since generics allow us to implement several IEnumerable<T> interfaces on a single class, I think that "foreach" should somehow reflect that. suppose we have a enumerable class class C :...
7
by: Osiris | last post by:
Just something I would like to share: I just learned the hard way (2 days detective work on a bug) that foreach loops are not at all like for loops, not intuitive at all. BEWARE: arrays and...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.