A Class in Composition

bearophile bearophileHUGS at lycos.com
Tue Aug 27 07:38:20 PDT 2013


Chris:

> Another drawback is that ranges demand a lot of boilerplate 
> code. If properly implemented, there is a lot of code you have 
> to write over and over again such as
>
> if (isInputRange!Range && isInputRange!(ElementType!Range) &&
>         is(ElementType!(ElementType!Range) == MyType))
>
> I don't know if this could possibly be reduced. Or maybe it's 
> just my lack of experience.

Two ways to reduce "boilerplate code" for that code:

One way to reduce "boilerplate code" is to introduce in D a 
C#-style "yield" that's syntax sugar to create Input Ranges:

yield!int inverseGrayCode() {
     yield 0;
     foreach (x; inverseGrayCode())
         if (x & 1) {
             yield 2 * x + 1;
             yield 2 * x;
         } else {
             if (x)
                 yield 2 * x;
             yield 2 * x + 1;
         }
}


The yield keyword could also be used to denote a range of a type, 
to reduce this code of yours:

if (isInputRange!Range && isInputRange!(ElementType!Range) &&
     is(ElementType!(ElementType!Range) == MyType))

Bye,
bearophile


More information about the Digitalmars-d mailing list