Kotlin for Lunch - Execution After Return

Christoph Frei
The Startup
Published in
3 min readSep 9, 2020

“Kotlin for Lunch” collects Kotlin exercises, usage patterns, or “the Kotlin way” to solve development issues. This article should be short enough to have fun with Kotlin during a short lunch break.

We are here today: Kotlin > Scope Functions

The scenario:

We have a function that returns the reference to an object and then needs to update that very reference after it is returned.

Here is a naive and simple implementation of that idea. It doesn’t work. After return Kotlin doesn’t execute code. You get an “unreachable code” warning.

So the first working approach is simple; lets add a temporary variable to get that up and running. That’s good enough for production…

…but it doesn’t look nice, the temp variable is a technical crutch. So lets see if we can get rid of it.

Can we (miss?)use try…catch…finally and do the modification in that last finally block?

This works, and… looks a bit better than the temp variable, but we have more lines. We can write it in one line by making it a lambda, but doesn’t make it much better.

We know that the try is not needed, that code smells at bit. But if we would have exception handling already in the code, we would probably leave it like this.

Lets go back to the original idea and go a bit more crazy. We need a temp variable. There is something in the Kotlin handbook that exactly offers that. I can be found in the documentation for the operator inc(), that is utilized for the special symbol ++.

Kotlin (as basically all other languages) distinguish between the postfix ++ and the prefix ++. var j = 0; var i = j++ gives us i=0 and j=1. var j = 0; var i = ++j creates i = 1 and j = 1.

How does Kotlin do this? Here from that handbook:

And “storage a0” is exactly the temp variable that we can utilize. Lets try it out. Our inc()-operator just needs to return the next element, which is then assigned to itself. Next solution looks like that:

We can make it more elegant be moving the operator definition out of the function. And if we could change the whole interface, make inc() public, and everyone could just call next++ instead of forward().

Our function a very nice one-liner. But still — introducing an operator and somehow interpret ++ as the same as “move to the next element” smells still a bit…

The Kotlin syntax is not at its limits here — we have another solution: also for the win! Lets return next, but execute some code also(!) before returning.

This seems the most elegant implementation of code execution “after” return.

And if you want to play around, here a working example with all functions.

Creates the following output:

Current next name: b
Current next after calling forward: b
Current next after calling forward1: c
Current next after calling forward2: d
Current next after calling forward3: e
Current next after calling forward4: f
Current next after calling forward5: g
Current next after calling forward6: h
Current next after calling forward7: i

If you know another way to do it, no matter if more or less elegant, please let me know. Comments welcome. :-)

--

--