
Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
>> apply( new Callback() {
>> void callback(Object ... args) {
[quoted text clipped - 7 lines]
> Not nice? It illustrates what happens quite clearly, IMO, and makes life
> easier for documentation purposes.
Hear, hear! There is a tension between what programmers like to do during
development, which pushes toward economy of expression, and operational
concerns, where good logging and readable code are paramount. Anonymous
classes favor the maintainer, but a few developers tend to whine about them.
They should spend some time on undocumented code without anyone handy who was
on its original team.
> The closures feature is still one in hot debate; the last time I checked
> (admittedly several months ago), there were two or three different,
> non-compatible varieties of closures. It also appears that a majority of
> Java developers are at best indifferent to it or even downright hostile.
Count me in the latter.
> Joshua Bloch (I believe it was him) gave a presentation a few months ago
> as to why he thought that closures were ill-suited to Java, a position
> with which I agree.
Years ago Sun put out a white paper about why they chose anonymous classes
over delegates. It's somewhere on java.sun.com.
People in these newsgroups have presented quite sharp arguments as to why Sun
was wrong.
I do not dislike the delegate idiom - perhaps because I don't use C#. I do
like the anonymous class idiom, and various other places where Java uses
subtypes to do what closures or delegates would. Yes, I'm aware that closures
offer some syntax that inner classes don't, in particular with respect to
outer object variable visibility. In practice this has never held back the
effectiveness nor the expressiveness of my Java programs, so far.
If and when Java sports closures I will try them out. Perhaps they will woo
me over to the Dark Side.

Signature
Lew
Peter Duniho - 29 Mar 2008 08:26 GMT
>>> apply( new Callback() {
>>> void callback(Object ... args) {
[quoted text clipped - 12 lines]
> paramount. Anonymous classes favor the maintainer, but a few developers
> tend to whine about them.
And then...
> [...]
> I do not dislike the delegate idiom - perhaps because I don't use C#. I
> do like the anonymous class idiom, and various other places where Java
> uses subtypes to do what closures or delegates would.
It seems to me that you're mixing two different things up (as is Joshua),
if I read the message correctly.
I agree that it's nice to have the code that's being "passed" (whether as
a class implementing an interface or a delegate) present where it's used.
However, delegates in C# aren't mutually exclusive of that idea.
An interface implementation versus a delegate discusses _how_ the method
reference is passed. This is where Java forces the interface
implementation idiom on you, because it doesn't have a function pointer
type like delegates.
An anonymous implementation, whether passed as an interface implementation
or a delegate, discusses _what_ is passed and _where_ it's declared. This
is the idiom you appear to be saying you favor, but it's not missing from
C#.
C# has anonymous methods and, with C# 3.0, lamba expressions. Two very
convenient and self-documenting ways to create a delegate instance where
you need it (just like an anonymous class in Java), providing the all the
"illustration" and "documentation" advantages of an anonymous class,
without the disadvantage of having to actually have a whole new class
(such as some might consider that a disadvantage...I know I do, when the
class exists solely to contain a method to be called).
There are occasionally places where I think it'd actually be kind of nice
to be able to have an anonymous class in C#. It's not that there's
something wrong with that idea per se. But some times all you really need
is just one method, and for those situations, delegates work well (and
IMHO, in those situations they work better than classes, anonymous or
otherwise).
> [...]
> If and when Java sports closures I will try them out. Perhaps they will
> woo me over to the Dark Side.
If and when Java does that, I think it will have the best of both worlds.
Pete