[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Java



--- Ken Anderson <kanderson@bbn.com> wrote:
> >>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.

> P1: I agree with Scott, I wouldn't use the word nice here.  

Well, I mean "nice" in the sense that it fits elegantly into the
language, not in the "it's just like Scheme" mode.

> Before you define an anonymous class you have to define an interface
> or class.  

This is because of static type checking, not because Java closures are
classes.  You're getting a big feature in return for that extra bit of
code: compile time type errors.

> 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.

The primitive/Object design in Java has always mystified me, so I won't
defend it.

You use "_"... does that have some magic feature in Java I don't know
about, or was it just to make the method name short?

> 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

Well, they didn't all go to MIT, Rice, and Yale I guess.
 
> However, here is no mention of other features of using inner classes
> as closures or in any other remotely useful way.  

What they primarily get used for is dynamic event handlers, which is
one of the most widespread uses of closures outside the Scheme
community.  Closures are exactly what you want for dynamic event
handlers.  Java calls these things Listeners an you use them like:

final double String newLabel = "Pushed!";
final JButton button = new JButton();
button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent event) {
      button.setText(newLabel);
  }
});

// Note that "button" gets captured.

Java also has a neat but bizzare scope resolution syntax for getting to
the correct "this" pointer when inside a class/closure.  Inside the
event handler, "this" means the listener and "Foo.this" means the
enclosing class (if it was named Foo).

> 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.

Clever!
 
> P4:Why do computer languages still divide us so much?

I think they only do when people have tunnel vision.  I use several
different languages on a daily basis and have good and bad things to
say about all of them.  This makes me part of several user communities,
which is not at all a divisive thing.  

Many programmers seem to use a lot of different languages; it's only PL
people who pick favorites :)  Web programmers and system admins seem to
use every single language installed on a server, as needed. 
Application developers use at least Java and/or C++ plus Make, shell
scripts, and often a little embedded Python or Perl.

-m




__________________________________________________
Do You Yahoo!?
Check out Yahoo! Shopping and Yahoo! Auctions for all of
your unique holiday gifts! Buy at http://shopping.yahoo.com
or bid at http://auctions.yahoo.com