Multiple return types from function

You are thinking about tuples I guess. There are already implementations for Pair and Triple in kotlin. If you need more you can simply define them yourself, but using more than 3 values without naming them might get complicated and confusing real fast.
I’m not sure that there is a need for a special syntax there. I would love to see them converted to value types if we ever get project valhalla (probably it’s not as easy as just changing it due to binary compatibility but that’s not important here).

1 Like

In Rust, you can use tuple

// Tuples can be used as function arguments and as return values
fn reverse(pair: (i32, bool)) -> (bool, i32) {
    // `let` can be used to bind the members of a tuple to variables
    let (integer, boolean) = pair;

    (boolean, integer)
1 Like

People really need to start reading threads before posting to them. Please see @Wasabi375 comment.

1 Like

I have been extensively working with this kind of issue since quite a long time now and I came up with two other nice solutions other than destructurization and wrapper classes:

  • use references, like C/C++ pointers: KMutableProperty0<T>, unfortunately it doesnt work for local properties
var int = 0
fun modifyInt(pInt: KmutableProperty0<Int>) {
   var int by pInt // in this way you can use it inside the function like a normal var!
   int = 4

Or for other scenarios, like in a vulkan wrapper I’m working on, where I automatically check for the return result enumerator.
It may happen that sometimes I want to personally check this return result against very specific values, which are not errors, then in this case I simply pass a lambda as last parameter, which will be given the result enum:

infix fun <R>VkPhysicalDevice.createDevice(createInfo: VkDeviceCreateInfo, block: (VkResult) -> R): R {
    val result =
    return block(result)
1 Like

Hi. I’ve faced a similar problem this week. Then, I just returned a String with the 2 points, just like that:

fun getCoords(someData: Any) {
  val x: Double
  val y: Double

  // do stuf

  return "$x:$y"

and when I need to use the data, I just split that

This only works for types that can easily be parsed from a string. In most cases this doesn’t work. Just use Pair or Tripple or even better a custom data class so you can name the returned values. This also allows for far easier access than spliting and parsing a string


This is very, very dangerous:

  1. Return type should be parsed from String. Moreover, it should not have ‘:’ inside. This could not be verified by using strong typing.
  2. This return requires new String allocation. Because of variable return size, JVM will not allocate this on stack (in the most cases). E.g. if you return data class, JVM can avoid heap allocation. However this works for types with predefined size (for current moment of course).
  3. Moreover, just for toString execution you have to allocate a lot of another Strings (because "$x:$y" requires toString call from x and from y)

Finally: in general case it is better create separate class with signature like class Coordinates(val x: Double, val y: Double). It is just one line of code, however it works faster and allow you to do strong typing verification.


I used String return separated by : because that was what I need in my use case. And this method is inside a class with attributes x and y.

Well it looked like you were presenting a universal solution. If you need the string, that’s great and works for you, but if you need both values (separate from the string) you should return a pair and generate the string outside of the function. This is far more efficent.