yield, C# etc

Denis Koroskin 2korden at gmail.com
Wed Aug 13 05:12:54 PDT 2008


On Wed, 13 Aug 2008 15:49:58 +0400, bearophile <bearophileHUGS at lycos.com>  
wrote:

> In normal D:
>
> struct A006068b {
>     int opApply(int delegate(ref int) dg) {
>         int result, aux;
>         aux = 0; result = dg(aux); if (result) return result;
>         foreach(x; A006068b()) {
>             if (x & 1) {
>                 aux = 2 * x + 1; result = dg(aux); if (result) break;
>                 aux = 2 * x; result = dg(aux); if (result) break;
>             } else {
>                 if (x)
>                     { aux = 2 * x; result = dg(aux); if (result) break; }
>                 aux = 2 * x + 1; result = dg(aux); if (result) break;
>             }
>         }
>         return result;
>     }
> }
>
> The D version is bad to read and bad to write, there's too much noise.
>
> Bye,
> bearophile


template yield(char[] value)
{
     const char[] yield = "{ int aux = " ~ value ~ "; int res = dg(aux); if  
(res) { return res; } }";
}

struct A006068b {
     int opApply(int delegate(ref int) dg) {
         mixin(yield!("0"));
         foreach(int x; A006068b()) {
             if (x & 1) {
                 mixin(yield!("2 * x + 1"));
                 mixin(yield!("2 * x"));
             } else {
                 if (x) {
                     mixin(yield!("2 * x"));
                 }
                 mixin(yield!("2 * x + 1"));
             }
         }

         return 0;
     }
}

I hope we will be able to write yield(2*x + 1); instead of mixin soon (as  
an AST macros or a built-in feature).



More information about the Digitalmars-d mailing list