When with tailrec function


Why tailrec fun do no work with when? It has else branch as if but more clear syntax.


Why do you think it doesn’t work with when?

Here is an example to test the Collatz Conjecture for any number using it:

val two = BigInteger("2")
val three = BigInteger("3")

tailrec fun collatz(a: BigInteger): Unit
    = when
        a == BigInteger.ONE -> Unit
        a.mod(two) == BigInteger.ZERO -> collatz(a / two)
        else -> collatz(a * three + BigInteger.ONE)

(the Collatz Conjecture is that this method will terminate for any positive integer, which has not been proven)