www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - foreach

reply Rodolfo Borges <Rodolfo_member pathlink.com> writes:
http://www.digitalmars.com/d/statement.html#foreach

says:
ForeachStatement:
foreach (ForeachTypeList; Expression) Statement

//shouldn't it allow:
foreach(int i; [ 1, 2, 3, 4 ]) {
}

//not pretty to force me write:
static int[] dummy = [ 1, 2, 3, 4 ];
foreach(int i; dummy) {
}
Jun 14 2005
next sibling parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Rodolfo Borges" <Rodolfo_member pathlink.com> wrote in message 
news:d8mi9d$v48$1 digitaldaemon.com...
 http://www.digitalmars.com/d/statement.html#foreach

 says:
 ForeachStatement:
 foreach (ForeachTypeList; Expression) Statement

 //shouldn't it allow:
 foreach(int i; [ 1, 2, 3, 4 ]) {
 }

 //not pretty to force me write:
 static int[] dummy = [ 1, 2, 3, 4 ];
 foreach(int i; dummy) {
 }
The issue is that [1,2,3,4] isn't parsed as an expression returning a constant static array. One challenge with changing D to support inline arrays like that is determining the type of the contents. There have been posts in the past that show how to write a varargs function to construct a dynamic array at runtime. Here's a variation on those functions that uses the new typesafe varargs style: // Packages inputs into a dynamic array // pronounced tuh-dah accompanied by a sweeping gesture template toda(T) { T[] toda(T[] x...) { return x.dup; } } // example import std.stdio; int main() { foreach(int n; toda!(int)(1,2,3,4)) { writefln(n); } return 0; }
Jun 14 2005
next sibling parent reply "Uwe Salomon" <post uwesalomon.de> writes:
Perhaps you should add that this is a very cumbersome and slow approach.  
Do not try this at home! :)

Ciao
uwe
Jun 14 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Uwe Salomon" <post uwesalomon.de> wrote in message 
news:op.ssc2fc1e6yjbe6 sandmann.maerchenwald.net...
 Perhaps you should add that this is a very cumbersome and slow approach. 
 Do not try this at home! :)
why not? Do you mean that it allocates memory every time you call toda? What approach do you suggest?
Jun 14 2005
parent reply "Uwe Salomon" <post uwesalomon.de> writes:
On Tue, 14 Jun 2005 15:18:57 +0200, Ben Hinkle <ben.hinkle gmail.com>  
wrote:

 "Uwe Salomon" <post uwesalomon.de> wrote in message
 news:op.ssc2fc1e6yjbe6 sandmann.maerchenwald.net...
 Perhaps you should add that this is a very cumbersome and slow approach.
 Do not try this at home! :)
why not? Do you mean that it allocates memory every time you call toda? What approach do you suggest?
Yes, that's exactly what i mean. I would suggest that: for (size_t i = 1; i <= 4; ++i) If you need some more complex array: static size_t[4] indizes = [9, 23, 1, 7]; //... foreach (size_t i; indizes) But i have never needed something like that in my code. More likely is a dynamic array (“dynamic” means its contents, not necessarily its size), which can be traversed with the same foreach. Ciao uwe
Jun 14 2005
parent reply "Ben Hinkle" <bhinkle mathworks.com> writes:
"Uwe Salomon" <post uwesalomon.de> wrote in message 
news:op.ssc6gwup6yjbe6 sandmann.maerchenwald.net...
 On Tue, 14 Jun 2005 15:18:57 +0200, Ben Hinkle <ben.hinkle gmail.com> 
 wrote:

 "Uwe Salomon" <post uwesalomon.de> wrote in message
 news:op.ssc2fc1e6yjbe6 sandmann.maerchenwald.net...
 Perhaps you should add that this is a very cumbersome and slow approach.
 Do not try this at home! :)
why not? Do you mean that it allocates memory every time you call toda? What approach do you suggest?
Yes, that's exactly what i mean. I would suggest that: for (size_t i = 1; i <= 4; ++i) If you need some more complex array: static size_t[4] indizes = [9, 23, 1, 7]; //... foreach (size_t i; indizes)
I assumed the OP knew about for loops and they explicitly said they knew about the static array solution. I was giving an update to an old request about being able to construct arrays on-the-fly.
 But i have never needed something like that in my code. More likely is a 
 dynamic array ("dynamic" means its contents, not necessarily its size), 
 which can be traversed with the same foreach.
You can change the content of static arrays, too. I've always interpretted the "dynamic" in dynamic arrays to mean the length is only known at run-time (ie - dynamically).
Jun 14 2005
parent "Uwe Salomon" <post uwesalomon.de> writes:
 You can change the content of static arrays, too. I've always  
 interpretted
 the "dynamic" in dynamic arrays to mean the length is only known at  
 run-time (ie - dynamically).
You're right, of course. I forgot about that. Ciao uwe
Jun 14 2005
prev sibling next sibling parent reply pragma <pragma_member pathlink.com> writes:
In article <d8mjg5$vuo$1 digitaldaemon.com>, Ben Hinkle says...
Here's a variation on those functions that uses 
the new typesafe varargs style:

// Packages inputs into a dynamic array
// pronounced tuh-dah accompanied by a sweeping gesture
template toda(T) {
  T[] toda(T[] x...) {
    return x.dup;
  }
}
Ben, just a thought: are you 100% certain that you *must* dup the array? - EricAnderton at yahoo
Jun 14 2005
parent "Ben Hinkle" <bhinkle mathworks.com> writes:
"pragma" <pragma_member pathlink.com> wrote in message 
news:d8mnnc$13f2$1 digitaldaemon.com...
 In article <d8mjg5$vuo$1 digitaldaemon.com>, Ben Hinkle says...
Here's a variation on those functions that uses
the new typesafe varargs style:

// Packages inputs into a dynamic array
// pronounced tuh-dah accompanied by a sweeping gesture
template toda(T) {
  T[] toda(T[] x...) {
    return x.dup;
  }
}
Ben, just a thought: are you 100% certain that you *must* dup the array? - EricAnderton at yahoo
From http://www.digitalmars.com/d/function.html#variadic it says it is an error to refer to the array after the variadic function has returned since the array might be constructed on the function's stack space.
Jun 14 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Ben Hinkle wrote:
 "Rodolfo Borges" <Rodolfo_member pathlink.com> wrote in message 
<snip>
//shouldn't it allow:
foreach(int i; [ 1, 2, 3, 4 ]) {
<snip>
 The issue is that [1,2,3,4] isn't parsed as an expression returning a 
 constant static array. One challenge with changing D to support inline 
 arrays like that is determining the type of the contents.
<snip> This isn't a request for array literals. It's a request to allow this bit of a ForeachStatement to be an array initialiser. Which suffers from this "challenge" to exactly the same extent as static int[] dummy = [ 1, 2, 3, 4 ]; does. Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit.
Jun 14 2005
prev sibling parent Phoenix <phoenix flareware.cz> writes:
Rodolfo Borges napsal(a):
 http://www.digitalmars.com/d/statement.html#foreach
 
 says:
 ForeachStatement:
 foreach (ForeachTypeList; Expression) Statement
 
 //shouldn't it allow:
 foreach(int i; [ 1, 2, 3, 4 ]) {
 }
 
 //not pretty to force me write:
 static int[] dummy = [ 1, 2, 3, 4 ];
 foreach(int i; dummy) {
 }
 
 
you can use Python for this feature ;)
Jun 15 2005