weird behavior returning delegate

Bruno Medeiros brunodomedeirosATgmail at SPAM.com
Mon Jul 3 03:12:39 PDT 2006


Tom S wrote:
> Carlos Santander wrote:
>> Thanks for the answers. I was hoping D could be added to 
>> http://www.paulgraham.com/accgen.html only with three lines instead of 
>> something like that horrible C++ code.
> 
> It's not very bad in D:
> 
> 
> T delegate(T) acc(T)(T i) {
>     class Foo {
>         T x;
>         this() { x = i; }
>         T func(T a) { return x += a; }
>     }
>     return &(new Foo).func;
> }
> 
> or
> 
> T delegate(T) acc(T)(T i) {
>     auto res = new class Object {
>         T x;
>         T func(T a) { return x += a; }
>     };
>     res.x = i;
>     return &res.func;
> }
> 
> 

Also, if something like Markus Dangl partial application were available 
in the library, it could be this as well:

   T delegate(T) acc(T)(T i) {
      return &AdvancedDelegate(
          (T num, T inc) { return num + inc; }
        ) (i).Eval;
   }

:)

-- 
Bruno Medeiros - CS/E student
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D



More information about the Digitalmars-d mailing list