www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1642] New: static foreach support for arrays

reply d-bugmail puremagic.com writes:
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.


-- 
Nov 06 2007
next sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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.
Nov 06 2007
parent Christopher Wright <dhasenan gmail.com> writes:
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 :)
Nov 06 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
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
prev sibling parent d-bugmail puremagic.com writes:
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: -------
May 10 2010