Monday, January 7, 2008

Completion Transparency

Completion Transparency

Completion transparency enables us to create methods that transparently return the result of a closure which is passed as argument. For example:

  static <T> T withLock(Lock lock, { => T } block) {
    lock.lock();
    try {
      return block.invoke();
    } finally {
      lock.unlock();
    }
  }
  

We can pass to the withLock method a block that returns any value. For example:

  Lock lock = new ReentrantLock();
  Integer i = withLock(lock, { => Integer.valueOf(42) });
  

Due to completion transparency, we can also use a block that does not return anything (i.e. a block of type { => void }):

  withLock(lock, { => System.out.println("mining..."); });
  

And even a block that cannot complete normally, i.e. throws exception (in this case only unchecked exceptions are allowed):

  withLock(lock, { => throw new NullPointerException(); });
  

To allow checked exceptions as well, we can use exceptions transparency:

  static <T, throws E> T withLockEx(Lock lock, { => T throws E} block) 
    throws E {
    lock.lock();
    try {
      return block.invoke();
    } finally {
      lock.unlock();
    }
  }
  
  public static void main(String[] args) {
    try {
      Lock lock = new ReentrantLock();
      withLockEx(lock, { => throw new Exception(); });
    } catch (Exception e) { e.printStackTrace(); }
  }
  

Implementation issue

Let us recall that each closure is converted to an instance of interface. For example, a closure { => System.out.println("buff"); } is converted to an instance of V:

  public interface V {
    void invoke();
  }
  

To achieve completion transparency, the compiler is allowed to use Void instead of void. For example, the code

  withLock(lock, { => System.out.println("miaow"); });
  

will be transformed approximately as follows:

  withLock(lock, new javax.lang.function.O() {
    public Void invoke() {
      System.out.println("miaow");
      return null;
    }
  }
  

In case of closures that do not complete normally, the invoke method returns java.lang.Unreachable which is a special class that is considered a subclass of all classes. This means that an Unreachable may be assigned to any reference variable. For example, the statement

  Integer i = { => throw new NullPointerException(); }.invoke();
  

will be transformed roughly as follows:

  Integer i = new javax.lang.function.O() {
    public Unreachable invoke() { throw new NullPointerException(); }
  }.invoke();
  

3 comments:

Anonymous said...

Klik Aja Di Bawah Ini
Pemesanan: 087733776655
http://obatkuat-57.com/
Jual Obat Pembesar Penis Vigrx Plus
Alat Bantu Sex Pria Dan Wanita
Rajanya Obat Kuat Sex
Obat Perangsang Wanita, Obat Gairah Wanita
Obat Pembesar Payudara Monyok, Payudara Kencang
Alat Pembesar Payudara Montok
Alat Pembesar Penis
Alat Sex Pria Boneka Moderen
Obat Pengental Sperma
Minyak Pembesar Penis, Obat Pemanjang Penis
Obat Hernia Paling Ampuh
Obat Pelangsing Badan, Melangsingkan Tubuh
Obat Perapet Vagina
Obat Peninggi Badan, Meninggikan Bada
Obat Pembesar Pantat, Pembesar Bokong
Obat Pemerah Bibir, Pemerah Puting
Obat Pemutih Wajah Alami Tensung Cream
Obat Penggemuk Badan
Obat Penghilang Tatto Permanen Herbal Alami
Kondom Antik Pria
Rajanya Obat Kuat Pria
Alat Bantu Sex Wanita Antik
Alat Bantu Sex Pria
Obat Kuat Herbal
Vimax Obat Pembesar Alat Vital
Cara Memutihkan Selangkangan dan Ketiak


Obat Pelangsing Badan Fatloss Jimpness Beauty
Selaput Dara Perawan Buatan


Radley Co Tad said...

Very much useful article. Kindly keep blogging

Java Training in Chennai

Java Online Training India

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 NGAWI
JUAL OBAT ABORSI DI PAMEKASAN
JUAL OBAT ABORSI DI PACITAN
JUAL OBAT ABORSI DI PASURUAN
JUAL OBAT ABORSI DI BANGIL