Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 14, 2005 foreach | ||||
---|---|---|---|---|
| ||||
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) { } |
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rodolfo Borges | "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; } |
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Perhaps you should add that this is a very cumbersome and slow approach. Do not try this at home! :) Ciao uwe |
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uwe Salomon | "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? |
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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 |
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to pragma | "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. |
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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
|
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | 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. |
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Uwe Salomon | "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). |
June 14, 2005 Re: foreach | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | > 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
|
Copyright © 1999-2021 by the D Language Foundation