Re: Passing a method(reference) to an other method and calling the method.



On Fri, 28 Mar 2008 20:58:10 -0700, Lew <lew@xxxxxxxxxxxxx> wrote:

Joshua Cranmer wrote:
Jan Thomä wrote:
apply( new Callback() {
void callback(Object ... args) {
thisMethod(args[0], args[1]... );
}
});


Not very nice, but i hear closures are to come in one of the next Java
versions....
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.

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
.



Relevant Pages

  • Re: Java FACT ?
    ... primitive or reference values. ... variable to be visible to the anonymous class. ... Perhaps so, but I was speaking of how Java works currently, as opposed to how Java might work if it had closures. ...
    (comp.lang.java.advocacy)
  • Re: static hashtable with conent?
    ... Having no money invested in the reading of a post leaves the reader free to quit it at a moment's notice for any reason at all with no sense of having lost any investment. ... and certainly to avoid unreasonable demands), that Patricia, whose general good sense is, I should think, well established in these parts, at least to the extent that a history of posts can be said to establish a picture of their author, has the right of it here; that while the grammars that underlie both natural languages like English and artificial languages like Java are capable of forming expression and statements of arbitrarily high complexity, simply by applying the generation rules repeatedly, that in both cases the idioms which are understood easily and naturally come from a constrained application of those rules, and that using examples lying outside the standard idioms places upon our readers, whose ready understanding is in fact in our own interests as well as in theirs, an unnecessary burden, and thus should be avoided or at least severely minimized, absent any significant advantage to be found in their use. ... In some rare cases, although it has yet to be necessary for any program I've worked on, I can imagine needing to define an anonymous class constructor. ... It's just that in Java the anonymous class idiom is the heart of certain closure-like expressions. ...
    (comp.lang.java.programmer)
  • Re: Delegates...?
    ... Buried in that article is actually an example of how a behavior similar to delegates in C# can be implemented in Java. ... I don't really know that this is the _best_ way, but it is how I've been dealing with the lack of delegate types, and the article Lew posted appears to suggest that's in fact how the Java designers expect you deal with it. ... private event EventDelegate Test; ... public void SubscribeTestEvent ...
    (comp.lang.java.programmer)
  • Re: Is Ruby On Rails, Delphis lost cousing?
    ... get Java semantics but only if necessary): ... given "powerful" libraries, but there's a serious core to that argument. ... yield return e.Current; ... Benchmark("Creation", 10000, delegate ...
    (borland.public.delphi.non-technical)
  • Re: Method invocation via proxy and reflection
    ... default SM and you'll see an exception. ... The result "147" is because Java usually optimizes method invocations. ... CharSequence, so instead of expected invocation of lengthin Delegate, ...
    (comp.lang.java.programmer)