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

10 comments:

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

Anonymous 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());
}

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

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

Plumbing & HVAC Services San Diego said...

Compre documentos en línea, documentos originales y registrados.
Acerca de Permisodeespana, algunos dicen que somos los solucionadores de problemas, mientras que otros se refieren a nosotros como vendedores de soluciones. Contamos con cientos de clientes satisfechos a nivel mundial. Hacemos documentos falsos autorizados y aprobados como Permiso de Residencia Español, DNI, Pasaporte Español y Licencia de Conducir Española. Somos los fabricantes y proveedores de primer nivel de estos documentos, reconocidos a nivel mundial.

Comprar permiso de residencia,
permiso de residenciareal y falso en línea,
Compre licencia de conducir en línea,
Compre una licencia de conducir española falsa en línea,
Comprar tarjeta de identificación,
Licencia de conducir real y falsa,
Compre pasaporte real en línea,

Visit Here fpr more information. :- https://permisodeespana.com/licencia-de-conducir-espanola/
Address: 56 Guild Street, London, EC4A 3WU (UK)
Email: contact@permisodeespana.com
WhatsApp: +443455280186