digitalmars.D - anonymous delegate syntactic sugar
- Ben Hinkle (35/35) Jul 11 2004 I was experimenting with implementing simple iterators as anonymous
- Matthias Becker (9/18) Jul 11 2004 What about something more general?
- Ben Hinkle (6/28) Jul 11 2004 Well, the keyword "delegate" is meant for this purpose, but it's true th...
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
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
Matthias Becker wrote: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.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








Ben Hinkle <bhinkle4 juno.com>