www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - anonymous delegate syntactic sugar

reply Ben Hinkle <bhinkle4 juno.com> writes:
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
Jul 11 2004
parent reply Matthias Becker <Matthias_member pathlink.com> writes:
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.
Jul 11 2004
parent Ben Hinkle <bhinkle4 juno.com> writes:
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.
Jul 11 2004