Type checking recursive problem with expression body


#1

I’m hitting a strange type checking recursive problem when using expression bodies.

// This is ok:
fun Int.recursive() {
	init {
		if (this > 0)
			(this - 1).recursive()
	}
}

// But this is not:
fun Int.recursive() = init {
	if (this > 0)
		(this - 1).recursive() // Type checking has run into a recursive problem
}

fun init(inner: () -> Unit) {
	return inner()
}

I suspect this is intentional, but I don’t quite understand why the recursion doesn’t work with an expression body here.


#2

I think just to keep the impl simple, they just require that any circular function ref have an explicit return type. Just add the explicit :Unit return type (or, better as a warning will probably tell you, don’t use expression/equals func body syntax, instead use the brace syntax when returning Unit and you don’t have this problem).


#3

The return type pf init is fixed though. The compiler should be able to tell the return type of recursive just from that. There is no need to analyze the lambda passed to init.

I think you should post this as a bug at the issue tracker: https://youtrack.jetbrains.com/issues/KT