Would it be possible for inline functions like `le...
# language-proposals
n
Would it be possible for inline functions like
let
,
apply
,
also
, etc. to work with tail recursive functions? For example, I’d have thought the following two functions were the same, but the
foo2
is not tail recursive.
Copy code
tailrec fun foo1(acc: Int, x: Int?): Int? =
    if (x == null) {
		null
    } else if (x <= 0)  {
        acc
    } else {
        foo1(acc+1, x-1)
    }


tailrec fun foo2(acc: Int, x: Int?): Int? =
    x?.let {
        if (it <= 0)  {
        	acc
    	} else {
        	foo2(acc+1, it-1)
    	}
    }
p
Theoretically yes. But it would require either let etc to be compiler intrinsics; the compiler to be able to understand and analyse bytecode; or for inline functions to not be stored as bytecode but as AST representation. The AST representation has other optimization opportunities, but currently inline doesn't work that way (it basically works through bytecode wrangling)