kotlin.Result as return type in function literal

Its known that kotlin.Result can’t be used as return type for functions;
So fun parseNumber(s: String) = runCatching { s.toInt() } will not compile.

But what I find weird is that it can be used for function literals, as follow:

val parseNumber: (String) -> Result<Int> = { n -> runCatching { n.toInt() } }


val parseCatchingFn: (String) -> Result<Int> = fun(n) = runCatching { n.toInt() }

will compile correctly, why so and what’s the difference?

Edit: Sorry, I think my question was not clear enough. My question is not about the reasons why it’s not allowed in the first place,
I’m asking why it allowed for function literals, is there any difference?

1 Like

The rationale behind this strange limitation:

TLDR: java interop strikes again