Thread overview
[Issue 1642] New: static foreach support for arrays
Nov 06, 2007
d-bugmail
Nov 07, 2007
Daniel Keep
Nov 07, 2007
Christopher Wright
May 10, 2010
Simen Kjaeraas
November 06, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1642

           Summary: static foreach support for arrays
           Product: D
           Version: 2.007
          Platform: PC
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P3
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: dhasenan@gmail.com


The following is required to be executed at compile time, and all data is available at compile time:

int foo () {
    foreach (string letter; ["a", "b", "c"])
        pragma (msg, letter);
    return 0;
}
static const int i = foo();

The expected output when compiling is:
a
b
c

The actual result when compiling is:
foreach_array.d(5): Error: string expected for message, not 'letter'

The issue: static foreach only works for tuples, and I want to use it on an array.

This is inconsistent with __traits: the allMembers trait returns an array. If it returned a more obtuse structure, a tuple, then I wouldn't need to use recursion with it; and if static foreach supported arrays, that would make even more code more readable.


-- 

November 07, 2007
Currently, there's no such thing as "static foreach".  That the foreach is unrolled at compile-time is simply a side-effect of the aggregate being a tuple.

Walter has already indicated that a true static foreach is planned for 2.x.
November 07, 2007
Daniel Keep wrote:
> Currently, there's no such thing as "static foreach".  That the foreach
> is unrolled at compile-time is simply a side-effect of the aggregate
> being a tuple.
> 
> Walter has already indicated that a true static foreach is planned for 2.x.

Now he has a ticket to keep track of it, then :)
May 10, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1642


Simen Kjaeraas <simen.kjaras@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com


--- Comment #2 from Simen Kjaeraas <simen.kjaras@gmail.com> 2010-05-10 01:51:48 PDT ---
This template lets you iterate through a compile-time array as a tuple. Not as good as the real thing, but might be worth having around.

template ArrayToTuple( alias T ) {
    static if( T.length > 1 ) {
        alias TypeTuple!( T[ 0 ], ArrayToTuple!( T[ 1..$ ] ) ) ArrayToTuple;
    } else {
        alias TypeTuple!( T[ 0 ] ) ArrayToTuple;
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 10, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=1642


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #3 from bearophile_hugs@eml.cc 2010-05-10 03:55:47 PDT ---
See also bug 4085

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------