...

/

Functional Programming: Tail-Call Optimization

Functional Programming: Tail-Call Optimization

Learn about tail-call optimization in Java 8.

What is Tail-Call optimization?

One of the hallmarks of functional programming is tail-call recursion. It solves the same problem as iterative approaches . Unfortunately, it can cause stack-overflows if not properly optimized by the compiler.

Tail-Call optimization refers to when a compiler converts a recursive function call into a loop to avoid using the call stack. For example, a function that uses tail-call recursion in Lisp will be automatically optimized this way.

Java 8 support

Java 8 does not currently support tail-call optimization like some other languages. However, it is possible to approximate it using something like the following interface:

@Functiona
...