Tailrec in get() functions?


#1

Hi,

I’m wondering if there is any particular reason, why the tailrec keywoard cannot be applied to get() functions.

I’m trying to add an extension property Throwable.rootCause which is an excellent candidate for tail recursion, but I can only use tail-recursion when I define it as a function:

tailrec fun Throwable.getRootCause() : Throwable {
    val c = this.cause
    return if (c == null) this else c.getRootCause()
}

It doesn’t work to make a tailrec getter function:

val Throwable.rootCause: Throwable
tailrec get() {
    val c = this.cause
    return if (c == null) this else c.rootCause

}

The error is: "modifier ‘tailrec’ is not applicable to ‘getter’.

The workaround is relatively easy but it made me wonder if there was any particular reason for this restriction, or any plans to lift it (couldn’t find any with a quick search but I didn’t make an exhaustive search).