[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
RE: Java
At 12:34 PM 12/10/2001, Scott McKay wrote:
>At 12:30 PM 12/10/01, Morgan McGuire wrote:
>>> >____ is so much more convenient and easy to use than Java
>>>
>>> * First-class functions
>>
>>FYI, Java 2's anonymous classes nicely fill the gap made by first class
>>functions. In Java, you can create a closure and pass it around just
>>the way you would with a first class function. Considering the
>>language doesn't *have* functions, using classes as closures makes more
>>sense than introducing functions, although it is a little verbose.
>
>
>Actually, I dispute "nicely". I've just been rewriting a very "functional"
>Dylan program in Java for my own entertainment, and the "replace a
>first-class function with an anonymous class" has been the single
>most annoying thing I have had to do.
>
>What could be nicer than
> do-lines(write-line, buffer) ?
Several points:
P1: I agree with Scott, I wouldn't use the word nice here. Before you define an anonymous class you have to define an interface or class. This is more work than saying (lambda (x) ...). You also have to decide how complex the interface or class is going to be. For example, because of the primtive/Object divide you need to decide if your interface should look like:
public interface Predicate {
public boolean _(Object a, Object b);
}
So to use this on primitives you need to coerce the primitives to objects.
You could implement more variations on the predicate method _(,) but that makes the user work harder to write code to match the interface.
P2: When Java developers get a chance to write about this stuff they relate it to "function pointers" of C/C++ (is this always 1?). See for example:
http://developer.java.sun.com/developer/JDCTechTips/2001/tt1106.html
The article shows how to use the Java equivalent of function pointer to make a comparator to sort a sequence, and describes the beginnings of the Visitor pattern which is import only because we are programming in Java. (Common Lisp, for example, doesn't need this pattern because it can add behavior to a class at any time.)
However, here is no mention of other features of using inner classes as closures or in any other remotely useful way.
This would be a great learning opportunity to say something about closures, functional programming, even continuation passing, that might enrich a Java programmers life. Why did Sun miss this opportunity?
P3: In Lisp and Scheme, at least, closures are opaque. Instances of classes used functionally are less opaque because they have additional behavior than application.
So, for example, you could have a version of function composition that does peephole optimization, for example.
I think this is an example of a "lost pattern" because while it is written in Java, Java programmers don't think of it. Functional programmers don't think of it even though they understand how to use it once they cross the divide.
P4:Why do computer languages still divide us so much?
- Follow-Ups:
- RE: Java
- From: Morgan McGuire <morgan3d@yahoo.com>
- References:
- RE: Java
- From: Morgan McGuire <morgan3d@yahoo.com>
- RE: Java
- From: Scott McKay <swm@hotdispatch.com>
- RE: Java
- From: Scott McKay <swm@hotdispatch.com>