Saturday, March 29, 2008

Method references (Implementation Issue)

Method references (Implementation Issue)

Each method reference is transformed to a closure:

  // Math#sqrt(double) will be transformed to { double d => Math.sqrt(d) }
  { double => double } sqrt = Math#sqrt(double);

Instance method references are transformed in a similar way.

  Circle c = new Circle();
  { => void } p = c#draw();  // the same as { => c.draw(); }
  { Circle => void } p = Circle#draw();  // { Circle c => c.draw(); }

Let us have a look at another example. We declare class Box:

  public class Box<T> {
    T v;
    public Box(T v) {
      this.v = v;
    public T getValue() {
      return v;
    public void print() {
      System.out.println("Box: " + v);

and refer to the print method:

  // Box#print() will be transformed to { Box<Integer> box => box.print(); }
  { Box<Integer> => void } p = Box#print();

Reference to a generic method will be replaced by a closure as follows:

  public class Generic {
    public static <T> Box<T> copy(Box<T> b) {
        return new Box<T>(b.getValue());
  // Generic#copy(Box<Integer>) will be transformed to 
  // { Box<Integer> box => Generic.copy(box) }
  { Box<Integer> => Box<Integer> } boxCopy = Generic#copy(Box<Integer>);

I do not want closures in Java because they are not simple.
This argument is wrong. We should always weigh all pros and cons. Closures are not simple. But is software development in Java simple? What should I know to be capable to develop software in Java? Knowledge of the Java programming language is clearly not enough. One should also know some Java technologies and frameworks. And are they simple? Are JPA, EJB, JAX-WS, Hibernate, or Spring (to name a few) simple? Apparently no, so it does not make sense to reject closures just for this reason.


Luc Duponcheel said...

I would even want to go further than your statement about closures

IMHO there is this difference between

simple versus complex
easy versus difficult

By the way: things that are complex may appear to be difficult

in the context of programming

simple/complex is mainly a syntactic thing
(syntax is about the form of programming language constructs)

easy/difficult is mainly a semantic thing
(semantics is about the meaning of programming language constructs)

q1: are closures complex?
a1: they are simpler that all other Java syntax that has the same semantics

q2: are closures difficult?
a2: well, that depends! I do not want to sound offensive to those who find them difficult, but the answer to this question depends on the capability of us, human beings, to understand a certain concept. Closures are certainly not making the concept more difficult than needed (because of (a1))

Go on doing this tutorial thing, Zdenek!


Anonymous said...

Great stuff, thanks a lot!

Felix said...

Hey, I don't know a better place to ask, is there a place where people put their closure powered code? Is there a place where I can put mine?

felix said...

Well now there is. I'd be glad if you'd pass by and leave comments.

Zdeněk Troníček said...

Hi felix,

sure, you can post a closure code here. Another place is perhaps Neal Gafter's blog.