Towards Functional Java: Closures

This is the third post in the Towards Functional Java series. In the previous posts we examined how to dynamically define functions for later execution using method references and lambda expressions. The capability to create and manipulate functions at runtime is at the core of functional programming.

Sometimes a lambda expression may access variables that are defined outside of its scope. These are called free variables. Consider the following code:

public class Printer {
	
  public void printSubstring(String text, int beginIndex, int endIndex) { 
    Runnable r = () -> {
      System.out.println(text.substring(beginIndex, endIndex));
    };	
    new Thread(r).start();
  }

  public static void main(String[] args) {
    Printer printer = new Printer();
    printer.printSubstring("Hello!", 0, 4);
    printer.printSubstring("My name is John", 3, 10);
    printer.printSubstring("How are you today?", 4, 11);
  }
}

 

The Runnable implementation accesses three variables that are not defined in the scope of the lambda expression: text, beginIndex, endIndex. These variables are defined in the scope of the enclosing printSubstring method which may have terminated by the time the thread is executed. Normally when a method completes execution its local variables are removed from memory.

The program however executes correctly and the lambda expression somehow retains the values of the enclosing method arguments. A function or block of code along with the captured values of its associated free variables is called a closure.

Java lambda expressions are therefore closures but with an important restriction: they only close over values but not variables. If we try to modify the value of a free variable inside a lambda expression we get a compiler error. Let's modify the Runnable implementation slightly to demonstrate this:

Runnable r = () -> {
  System.out.println(text.substring(beginIndex, endIndex--));
};	

 

We have tried to decrement the value of the free variable endIndex in the lambda expression. The compiler complains with the following message:

Local variable endIndex defined in an enclosing scope must be final or effectively final

Although it is not required that free variables be declared final, effectively they are not allowed to change. In other functional programming languages there is no such restriction, but Java had to reach a compromise since to implement “real closures” would require a massive effort and possibly compromise backward compatibility and/or support for other JVM languages.

The “closure for immutable variables” implemented by Java 8 lambda expressions is a pragmatic solution that still achieves function portability without causing too much inconvenience to the programmer.

In the next post of this series we will look at more Java 8 functional features. Feel free to leave any comments or suggestions. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *