Multiple return types from function

I recently faced this problem in Java where a new point with x,y has to be returned by a function executed inside a draw() method. The disaster in Java is that returning a new Point object that only contains x and y numbers allocates quite some memory that is discarded after the method returns and there are many of these points created on each frame. A lot of memory allocations not needed for simply returning two values of the same type.

It will be much more efficient if Kotlin could recognize a tuple of the same type and create the most optimal data structure (plain old array?) to pass the values, so no extra memory than needed is allocated for the return values.

Is the current boxing/unboxing of return values in Kotlin memory efficient?

1 Like