재귀 호출되는 함수 앞에 trailrec modifier를 붙이고, formal condition을 만족하면, 컴파일러가 재귀 대신 loop 기반의 호출로 최적화 처리해준다.
trailec이 동작하기 위한 formal condition은 아래와 같다.
To be eligible for the
tailrec
modifier, a function must call itself as the last operation it performs. You cannot use tail recursion when there is more code after the recursive call, withintry
/catch
/finally
blocks, or on open functions. Currently, tail recursion is supported by Kotlin for the JVM and Kotlin/Native.
Infinite auto-scrolling lists with RecyclerView & LazyLists in Compose