A possible suggestion for the Foreach loop
Tyler Jameson Little
beatgammit at gmail.com
Tue Aug 20 20:22:35 PDT 2013
On Wednesday, 21 August 2013 at 02:46:06 UTC, Dylan Knutson wrote:
> Hello,
>
> I'd like to open up discussion regarding allowing foreach loops
> which iterate over a tuple of types to exist outside of
> function bodies. I think this would allow for templating
> constants and unittests easier. Take, for instance, this
> hypothetical example:
>
> ----------------------------------------------------------------------
> T foo(T)(ref T thing)
> {
> thing++; return thing * 2;
> }
>
> foreach(Type; TupleType!(int, long, uint))
> {
> unittest
> {
> Type tmp = 5;
> assert(foo(tmp) == 12);
> }
>
> unittest
> {
> Type tmp = 0;
> foo(tmp);
> assert(tmp == 1);
> }
> }
> ----------------------------------------------------------------------
>
> Without the ability to wrap all of the unittests in a template,
> one would have to wrap the bodies of each unittest in an
> individual foreach loop. This is not only repetitive and
> tedious, but error prone, as changing the types tested then
> requires the programmer to change *every* instance of the
> foreach(Type; TupleType).
>
> A similar pattern already exists in Phobos, for testing all
> variants of strings (string, dstring, and wstring) and char
> types, as eco brought to my attention. After taking a look at
> some of the unittests that employ this pattern, I'm certain
> that code clarity and unittest quality could be improved by
> simply wrapping all of the individual unittests themselves in a
> foreach as described above.
>
> Now, I'm certainly no D expert, but I can't think of any
> breakages this change might impose on the language itself. So,
> I'd like to hear what the benevolent overlords and community
> think of the idea.
Why not just do this?
T foo(T)(ref T thing)
{
thing++; return thing * 2;
}
unittest
{
void test(T)(T thing, T exp) {
assert(foo(thing) == exp);
}
foreach(Type; TypeTuple!(int, long, uint))
{
test!Type(5, 12);
test!Type(0, 1);
}
}
Unless you imagine doing this for something other than unittests.
More information about the Digitalmars-d
mailing list