Method annotation used to transform methods with tail recursive calls into iterative methods automagically since the JVM cannot do this itself. This works for both static and non-static methods.
It allows you to write a method like this:import groovy.transform.TailRecursive class Target {@TailRecursive
long sumUp(long number, long sum = 0) { if (number == 0) return sum; sumUp(number - 1, sum + number) } } def target = new Target() assert target.sumUp(100) == 5050 assert target.sumUp(1000000) == 500000500000 //will blow the stack on most machines when used without@TailRecursive
@TailRecursive
is supposed to work in combination with @CompileStatic
Known shortcomings:
Throwable
around a recursive might lead to problems
More examples:
import groovy.transform.TailRecursive @TailRecursive long sizeOfList(list, counter = 0) { if (list.size() == 0) { counter } else { sizeOfList(list.tail(), counter + 1) } } // Without @TailRecursive a StackOverFlowError // is thrown. assert sizeOfList(1..10000) == 10000