Weird errors with return inside lambda


#1

So in Kotlin 1.0 I was getting a runtime error on some code. Here is a stripped down version of my code that still demonstrates the problem:

private fun foo()
= File(“foo”).run()
{
return arrayOf(1, 2)
}

This failed at runtime saying it could not cast the array to java.lang.Void, but it gave a stack trace line number that did not exist so I had no idea where the actual error was.

To try to diagnose it I tried switching to 1.0.2 eap and it fails at compile time with:

'Nothing' return type needs to be specified explicitly

The line number is on the blank line before the function and the message was not very helpful but I was able to deduce that the error was due to the return statement. If I remove the return and just have the expression as the last line it works.

So I know the fix, just looking for some pointers to documentation on the thinking here regarding return statements inside of lambdas and is this a bug?


#2

There’s something in the chapter about inline functions: Non-local returns.

There are already an issue that might be related to this: KT-10717. KT-6295 is the change that required Nothing to be declared explicitely.