1.1. Closures
the definitions in this document are not necessarily the most formal ones one can think of typically simple, informal definitions are used in order to approach concepts from a didactical point of view
a closure is a block of code consisting of
a (possibly empty) list of parameter declarations, followed by
a (possibly empty) list of statements, and
an (optional) expression, (optionally) yielding the result of the block of code
closure syntax looks like
{ => statements }(no parameters, no expression){ parameters => statements }(no expression){ => expression }(no parameters, no statements){ parameters => expression }(no statements){ => statements expression }(no parameters){ parameters => statements expression }(general)
a closure ending with an expression is sometimes referred to as an expression closure
a closure not ending with an expression is sometimes referred to as a statement closure
a closure, say
closure, is invoked usingif the closure is an expression closure, then the invocations above are expressions, otherwise, if the closure is a statement closure, then the invocations above are statementsclosure.invoke()(no arguments)closure.invoke(arguments)(arguments)
a closure defines its own lexical scope containing
the names of the parameters of the closure
the names of the local variables defined in one of the statements of the closure
a closure can make use of the names of the variables from
its own lexical scope
enclosing lexical scopes
for the moment, only closures that do not make use of the names of the variables from enclosing scopes are dealt with, moreover, expression closures rarely contain statements
the following example shows statement closures
public class Closures01 {
public static void main(String[] args) {
// no parameters, no expression
{ => System.out.println("Hello World"); }.invoke();
// one parameter, no expression
{ String greetingToPlanet => System.out.println(greetingToPlanet); }.invoke("Hello World");
// two parameters, no expression
{ String greeting, String planet =>
System.out.println(greeting + " " + planet); }.invoke("Hello", "World");
}
}
the following example shows expression closures
public class Closures02 {
public static void main(String[] args) {
// no parameters, no statements
System.out.println({ => "Hello World" }.invoke());
// one parameter, no statements
System.out.println({ String greeting => greeting + " " + "World" }.invoke("Hello"));
// two parameters, no statements
System.out.println({
String greeting, String planet => greeting + " " + planet }.invoke("Hello", "World"));
// no parameters, one statement
System.out.println(
{ => System.out.print("Hello"); " " + "World" }.invoke());
// one parameter, one statement
System.out.println({
String separator =>
System.out.print("Hello"); separator + "World"}.invoke(" "));
// two parameters, one statement
System.out.println({
String separator, String planet => System.out.print("Hello"); separator + planet
}.invoke(" ", "World"));
}
}

1 comments:
Aha! This is really what I was hoping for. A simple, informal look from which I could infer the rules of the game. I'm sorry I didn't give these entries a look earlier. Thanks Luc!
Post a Comment