yield, C# etc
Don
nospam at nospam.com.au
Wed Aug 13 08:30:01 PDT 2008
Denis Koroskin wrote:
> 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).
Yup. And it'd be alright even without any AST stuff. Just something like:
mixin auto char[] yield(char [] value) {...}
to indicate that yield(x+2) automatically converts into mixin(yield("x+2"));
Then all the complicated AST stuff could be put in a library.
More information about the Digitalmars-d
mailing list