Thread overview
anonymous delegate syntactic sugar
Jul 11, 2004
Ben Hinkle
Jul 11, 2004
Matthias Becker
Jul 11, 2004
Ben Hinkle
July 11, 2004
I was experimenting with implementing simple iterators as anonymous delegates and it occurred to me that some syntactic sugar would make it more useful. I started with:

void foo(double delegate() next, int delegate() end) {
  while (!end())
    writefln(next());
}

int main() {
  static double[4] x = [10,20,30,40];
  int i=0;

  foo(delegate double() {return x[i++];},
      delegate int() {return i==x.length;});
  return 0;
}

and I thought if only the call to foo was simpler this might actually be
useful. My first though was to use the syntax
  delegate(<expr>)
as syntactic sugar for
  delegate typeof(<expr>)(){return <expr>;}
but that might not be a great idea since it looks very similar to
  delegate(parameter list){body}
In any case, if that syntax were used the call would look like
  foo(delegate(x[i++]),delegate(i==x.length));
which is much better than the original version.

It might not be worth it in the end, though, since creating and passing around pairs of next/end delegates is slightly more annoying than creating iterator instances (though it would reduce the amount of garbage the program generates).

Note there is already the syntax
  delegate{<stmt>}
as syntactic sugar for
  delegate void(){<stmt>}
so including delegate(<expr>) isn't too wierd.

-Ben

July 11, 2004
>My first though was to use the syntax
>  delegate(<expr>)
>as syntactic sugar for
>  delegate typeof(<expr>)(){return <expr>;}
>but that might not be a great idea since it looks very similar to
>  delegate(parameter list){body}
>In any case, if that syntax were used the call would look like
>  foo(delegate(x[i++]),delegate(i==x.length));
>which is much better than the original version.

What about something more general?

(<args>) => <body>

So your
foo(delegate(x[i++]),delegate(i==x.length));
becomes
foo(() => x[i++], () => i==x.length);

This would be a clean, nice synthax, IMO. And it would help to set D apart from C++, as things likes this are very unusual in C++, as they are way to complicated.




July 11, 2004
Matthias Becker wrote:

>>My first though was to use the syntax
>>  delegate(<expr>)
>>as syntactic sugar for
>>  delegate typeof(<expr>)(){return <expr>;}
>>but that might not be a great idea since it looks very similar to
>>  delegate(parameter list){body}
>>In any case, if that syntax were used the call would look like
>>  foo(delegate(x[i++]),delegate(i==x.length));
>>which is much better than the original version.
> 
> What about something more general?
> 
> (<args>) => <body>
> 
> So your
> foo(delegate(x[i++]),delegate(i==x.length));
> becomes
> foo(() => x[i++], () => i==x.length);
> 
> This would be a clean, nice synthax, IMO. And it would help to set D apart from C++, as things likes this are very unusual in C++, as they are way to complicated.

Well, the keyword "delegate" is meant for this purpose, but it's true that
less typing would be nice. At one extreme I could see the keyword
"delegate" being replaced with an "operator" symbol like @:
 foo(@x[i++],@(i==x.length))
Not very readable but short.