Tuesday, August 19, 2008

Nonlocal transfer

Nonlocal transfer

In unrestricted closures, we can use the break, continue, and return statements.

  for (int i = 0; i < 5; i++) {
    System.out.println(i);
    if (i == 2) { ==> break; }.invoke();
  }
  

These statements are always lexically bound, i.e. they are bound in the context in which the closure is declared (as opposite to invoked). So, the following code will print numbers from <1,10> that are not multiples of 3.

  static void m(int i, { int => boolean } cond, { ==> void } block) {
    if (cond.invoke(i)) {
      block.invoke();
    }
  }
  
  public static void main(String[] args) {
    for (int i = 1; i < 10; i++) {
      // continue is bound to the for loop
      m(i, { int i => i % 3 == 0 }, { ==> continue; });
      System.out.println(i);
    }
  }
  

The return statement returns from the bound method.

  static int m1() {
    // return here means "return from m1"
    m2({ double d ==> if (d < 0.5) return 1; });
    return 0;
  }
  
  static void m2({ double ==> void } p) {
    // the closure invocation can cause return from m1
    p.invoke(Math.random());
  }
  

If the bound method is not active at the time when we call the closure with the return statement, we will get the UnmatchedTransfer exception. For example:

  // any type can be used as the return type of p in this
  // example because the closure always returns earlier
  // so, p can be declared also as static { ==> int } p;
  static { ==> boolean } p;
  
  static int m1() {
    p = { ==> return 1; };
    return 0;
  }
  
  public static void main(String[] args) {
    System.out.println(m1());
    System.out.println(p.invoke());
  }
  

Unrestricted closures are useful when declaring "statement-like" methods.

  static void withLock(Lock lock, { ==> void } block) {
    try {
      lock.lock();
      block.invoke();
    } finally {
      lock.unlock();
    }
  }
  
  public static void main(String[] args) {
    Lock guard = new ReentrantLock();
    double d = Math.random();
    withLock(guard) {
      if (d < 0.5) {
        return;
      }
      ...
    }
  }
  

11 comments:

Denis Bredelet said...

This example is confusing. What is p.invoke() supposed to return?

static { ==> int } p;

static int m1() {
p = { ==> return 1; };
return 0;
}

public static void main(String[] args) {
System.out.println(m1());
System.out.println(p.invoke());
}


I would suggest:

static { ==> Nothing } p;
....
System.out.println(m1());
p.invoke();

Zdeněk Troníček said...

p.invoke() does not return anything because the closure always ends with the return statement that is bound to the enclosing method. The closure can have any return type (int, Nothing, void,...) in such case.

Denis Bredelet said...

That is why I wrote "This example is confusing". IMHO you need to revise it.

Zdeněk Troníček said...

Perhaps it is confusing but Nothing is not a better choice in my opinion.

Regarding the question "what is p.invoke() supposed to return?": it is expected to return int because p is of type { ==> int }. Even closures that are supposed to return a value can use the return statement. This is shown in 'p = { ==> return 1; };'. If you call such a closure in right context, the missing return value will not cause any problem. But if you call it in wrong context (as is done in the example), you will get the UnmatchedTransfer exception.

Zdeněk Troníček said...

Another example of UnmatchedTransfer exception:


static { ==> int } p;

static int m1() {
    p = { ==> if (Math.random() < 0.5) return 1; 2 };
    return 0;
}

public static void main(String[] args) {
    System.out.println(m1());
    System.out.println(p.invoke());
}

denis bredelet said...

I am not sure I should continue this discussion... You do not seem to see the problem.

In the latest example the semantics of p are "In the correct context,, if some random number is less than 0.5 then return from m1 with the value 1; otherwise the expression evaluates to 2."

Because both p and m1 return an integer, the type annotations in the example do not help readers to understand the semantics of p. In other words, that example is confusing. It relies on information found elsewhere (what does return do when it is found in a closure? what does a single value at the end of the closure body do?) for its interpretation.

A good example is self-documenting, does not rely on much prior knowledge from the reader for understanding. I suggest that using the appropriate type for the return value of p (in that case, Nothing) would make the example in the article better.

Well that was my last post here, I will quit complaining now.

Zdeněk Troníček said...

"what does return do when it is found in a closure?"

This is explained in the example that preceeds the example you find confusing.

"what does a single value at the end of the closure body do?"

The "confusing" example is not supposed to explain this. The example is, as is clearly stated in the text, about the UnmatchedTransfer exception.

As for Nothing, it is not common to use it as return type in a function type. Its primary use is in compiler generated code.

Dimitris Andreou said...

"int" is overloaded in this example, Dennis is right that this creates ambiguity.

"Nothing" (or anything apart from int) would easily make it clear as to where "return 1;" is actually returning.

Zdeněk Troníček said...

Hi Dimitris,

I agree with you that you must think when you read this example. But does it contain anything that has not been explained before? Definitely no because the semantics of 'return' should be known from the previous example.

Anyway, to make you happy, I changed the example slightly.

Toko Jual Obat Kuat Alat Bantu Sex 087733776655 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


iklan obatkuat said...

OBAT KUAT RANJANG. COM

Selamat datang di toko online kami,semoga kehadiran kami bisa membantu menyelesaikan masalah rumah tangga anda

Info pemesanan
call : 082133991881
BBm : 323fac0b

KAMI MENJUAL PRODUK -PRODUK DI BAWAH INI

VIMAX CANADA PEMBESAR PENIS

LINTAH PAPUA PEMBESAR PENIS

OBAT KUAT SEK

ALAT-ALAT SEX

PENIS TEMPEL ELEKTRIK

SEX TOYS PRIA DAN WANITA

PENIS MUTIARA GETAR GOYANG MAJU MUNDUR

VAGINA SILIKON MANUAL

VAGINA SENTER 7 SPEED

PENIS NAGA
ELEKTRIK


PERANGSANG WANITA CAIR

PENINGGI BANDAN

KONDOM DURI SILIKON

PEMBESAR PENIS

GALERI KOSMETIK

CELANA HERNIA

CONTAK KAMI :
Hub: 082133991881
Pin bb: 323fac0b





SITUS BOKEP ARTIS

TANTE TANTE KESEPIAN

VIDEO TANTE BUGIL