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.

9 comments:

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!

Luc

Anonymous said...

Great stuff, thanks a lot!

Anonymous 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?

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

klinik aborsi said...

Jual Obat Aborsi Asli | Obat Cytotec Asli | 082241083319
jual obat aborsi pills cytotec asli manjur untuk menggugurkan kandungan usia 1 - 6 bulan gugur tuntas.CYTOTEC OBAT ASLI sangat efektif mengatasi TELAT DATANG BULANdan menjadikan anda gagal hamil , CYTOTEC adalah OBAT ABORSI 3 JAM BERHASIL GUGUR TUNTAS dengan kwalitas terbaik produk asli pfizer nomor 1 di dunia medis
JUAL OBAT ABORSI DI SUMEDANG
JUAL OBAT ABORSI DI TASIKMALAYA
JUAL OBAT ABORSI DI BANJAR
JUAL OBAT ABORSI DI DEPOK
JUAL OBAT ABORSI DI PALEMBANG



surya said...

Really it was an awesome blog...very interesting to read..You have provided an nice information.
Angular JS Training in Chennai | Certification | Online Training Course | Angular JS Training in Bangalore | Certification | Online Training Course | Angular JS Training in Hyderabad | Certification | Online Training Course | Angular JS Training in Coimbatore | Certification | Online Training Course | Angular JS Training | Certification | Angular JS Online Training Course

Links For You said...

obviously a satisfying computation. i've confirmation this surprising broadcast. much thanks to you for sharing suggestion generally it. I in truth further to that. much obliged accurately parcel to your assemble. Microsoft Office Product Key Free

crackdj.com

hubert said...

Good Works. Keep it up.
https://thepcsoft.com/idimager-photo-supreme-crack/