digitalmars.D - foreach
- Rodolfo Borges (11/11) Jun 14 2005 http://www.digitalmars.com/d/statement.html#foreach
- Ben Hinkle (23/34) Jun 14 2005 The issue is that [1,2,3,4] isn't parsed as an expression returning a
- Uwe Salomon (4/4) Jun 14 2005 Perhaps you should add that this is a very cumbersome and slow approach....
- Ben Hinkle (4/6) Jun 14 2005 why not? Do you mean that it allocates memory every time you call toda? ...
- Uwe Salomon (13/19) Jun 14 2005 Yes, that's exactly what i mean. I would suggest that:
- Ben Hinkle (8/26) Jun 14 2005 I assumed the OP knew about for loops and they explicitly said they knew...
- Uwe Salomon (3/7) Jun 14 2005 You're right, of course. I forgot about that.
- pragma (3/12) Jun 14 2005 Ben, just a thought: are you 100% certain that you *must* dup the array?
- Ben Hinkle (5/18) Jun 14 2005 From http://www.digitalmars.com/d/function.html#variadic it says it is a...
-
Stewart Gordon
(13/19)
Jun 14 2005
- Phoenix (2/18) Jun 15 2005 you can use Python for this feature ;)
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
"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
Perhaps you should add that this is a very cumbersome and slow approach. Do not try this at home! :) Ciao uwe
Jun 14 2005
"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
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...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 uwePerhaps 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
"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: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."Uwe Salomon" <post uwesalomon.de> wrote in message news:op.ssc2fc1e6yjbe6 sandmann.maerchenwald.net...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)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?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
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
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
"pragma" <pragma_member pathlink.com> wrote in message news:d8mnnc$13f2$1 digitaldaemon.com...In article <d8mjg5$vuo$1 digitaldaemon.com>, Ben Hinkle says...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.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
Ben Hinkle wrote:"Rodolfo Borges" <Rodolfo_member pathlink.com> wrote in message<snip><snip>//shouldn't it allow: foreach(int i; [ 1, 2, 3, 4 ]) {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
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