weird behavior returning delegate
Chris Nicholson-Sauls
ibisbasenji at gmail.com
Sun Jul 2 22:22:43 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;
> }
>
Or even this:
# T delegate (T) foo (T) (T n) {
# with ( new class Object { private T x; public T acc (T i) { return x += i; } } ) {
# x = n;
# return &acc;
# }
# }
If in delegate literals we could declare static variables initialized from the local
frame, then it could even have simply been this:
# T delegate (T) foo (T) (T n) {
# return (T i) {
# static T x = n ;
# return x += i ;
# };
# }
Alas.
-- Chris Nicholson-Sauls
More information about the Digitalmars-d
mailing list