Multiple return types from function


#21

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).


#22

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)
}

#23

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


#24

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)
}