On 12/13/2010 12:23 AM, Adam D. Ruppe wrote:
Ary Borenszweig wrote:
D should provide a yield keyword that basically
rewrites the body of the method into the first code.

Don't need to change the language for that.

=========

string yield(string what) {
   return `if(auto result = dg(`~what~`)) return result;`;
}

class Foo
{
     uint array[2];

     int opApply(int delegate(ref uint) dg) {
          mixin(yield("array[0]"));
          mixin(yield("array[1]"));

        return 0;
     }
}
=========

Compiles and runs as expected, on today's D.

Cool!!

But it's kind of too much what you have to write. I like that you can do it without modifying the language, but sometimes it's better to have it directly in the language if it's you are going to use it often and it's useful. Remembering to put mixin() and enclosing the expression in a string is not very nice to the eyes.

I really like string mixins as they are inserted as is into the code, so there's no overhead in the call. It would be really cool if you could mark a function as being a string mixin, and then to do:

@mixin
string yield(string what) {
  return `if(auto result = dg(`~what~`)) return result;`;
}

class Foo
{
    uint array[2];

    int opApply(int delegate(ref uint) dg) {
         yield(array[0]);
         yield(array[1]);

        return 0;
    }
}

So that basically would work like this:

- @mixin tells the compiler it's a string mixin because the function returns a string. - In the semantic analysis the compiler needs to invoke yield(array[0]). It can see that yield is marked as @mixin, so instead of applying semantic analysis to array[0] it just converts it back to a string (I remember there exists such thing in DMD's source code) and then passes it to yield.

No more strings for string mixins! You get nice colors in your editor. You get autocomplete in those that support it.

Ideas?

Reply via email to