在 Kotlin,如何像在 Swift 中那样从函数返回多个值?

如何从 Kotlin 的一个函数返回3个相同类型(Int)的独立数据值?

我试图返回一天中的时间,我需要将小时,分钟和秒作为单独的整数返回,但是从同一个函数一次性返回,这可能吗?

我们迅速地这样做,

func getTime() -> (Int, Int, Int) {
...
return ( hour, minute, second)
}

我们能在 Kotlin 实现这一目标吗?

PS: 我知道我可以使用 Array 或者 Hashmap 来做这个,但是我想知道在 Kotlin 是否存在类似于 Swift 的东西。

81090 次浏览

According to the Kotlin documentation (https://kotlinlang.org/docs/reference/multi-declarations.html#example-returning-two-values-from-a-function) you can achieve it like that:

data class TimeData(val hour: Int, val minute: Int, val second: Int)


fun getTime(): TimeData {
// do your calculations


return TimeData(hour, minute, second)
}


// Get the time.
val (hour, minute, second) = getTime()

you can return multiple values like shown below

data class Result(val result: Int, val status: Status)
fun function(...): Result {
// computations


return Result(result, status)
}


// Now, to use this function:
val (result, status) = function(...)

see this documentation

for more example see this link

You can't create arbitrary tuples in Kotlin, instead, you can use data classes. One option is using the built in Pair and Triple classes that are generic and can hold two or three values, respectively. You can use these combined with destructuring declarations like this:

fun getPair() = Pair(1, "foo")


val (num, str) = getPair()

You can also destructure a List or Array, for up to the first 5 elements:

fun getList() = listOf(1, 2, 3, 4, 5)


val (a, b, c, d, e) = getList()

The most idiomatic way however would be to define your own data class, which allows you to return a meaningful type from your function:

data class Time(val hour: Int, val minute: Int, val second: Int)


fun getTime(): Time {
...
return Time(hour, minute, second)
}


val (hour, minute, second) = getTime()

There is no tuple in Kotlin. Alternatively, you can use data class with destructuring declaration.

data class Time(val hour: Int, val minute: Int, val second: Int)


func getTime(): Time {
...
return Time(hour, minute, second)
}


//Usage
val time = getTime()
println("${time.hour}:${time.minute}:${time.second}")
//Or
val (hour, minute, second) = getTime()
println("${hour}:${minute}:${second}")

If you don't want to create data class for each specific case, you may create some generic data classes and use typealias for clarity.

data class Two<A, B>(val a: A, val b: B)
data class Three<A, B, C>(val a: A, val b: B, val c: C)
data class Four<A, B, C, D>(val a: A, val b: B, val c: C, val d: D)
...


typealias Time = Three<Int, Int, Int>

But, obviously, the disadvantage is that you must make use of destructuring declaration in order to give it a proper property name.

val time = getTime()
println("${time.a}:${time.b}:${time.c}")


val (hour, minute, second) = getTime()    //destructuring declaration
println("${hour}:${minute}:${second}")

It seems that you are aware of the obvious answer of creating a specific class to handle time. So I guess you are trying to avoid the small hassle of creating a class, or accessing each element of an array, etc. and are looking for the shortest solution in terms of extra code. I would suggest:

fun getTime(): Triple<Int, Int, Int> {
...
return Triple( hour, minute, second)
}

and use it with deconstruction:

var (a, b, c) = getTime()

If you need 4 or 5 return values (you cannot deconstruct more than 5), go with Array:

fun getTime(): Array<Int> {
...
return arrayOf( hour, minute, second, milisec)
}

and

var (a, b, c, d) = getTime()

P.S.: you can use less variables than there are values when deconstructing, like var (a, b) = getTime() but you cannot use more or you'll get an ArrayIndexOutOfBoundsException

While, generally, a function can return only a single value, in Kotlin, by leveraging the benefits of the Pair type and destructuring declarations, we can return two variables from a function. Consider the following example:

fun getUser():Pair<Int,String> {//(1)
return Pair(1,"Mahabub")
}


fun main(args: Array<String>) {
val (userID,userName) = getUser()//(2)
println("User ID: $userID t User Name: $userName")
}

From: Functional Kotlin book

Use Triple to return 3 values and Pair to return 2 values

class Book(val title: String, val author: String, val year: Int) {




fun getTitleAuthor(): Pair<String, String> {
return (title to author)
}


fun getTitleAuthorYear(): Triple<String, String, Int> {
return Triple(title, author, year)
}}

If you are under special constraints and are looking to return multiple value without any class allocations, you may want to do something like this

val theBonus: Bonus
val theStuff = getStuff { theBonus = it }


doSomething(theStuff, theBonus)

where

import kotlin.contracts.*


@OptIn(ExperimentalContracts::class)
inline fun getStuff(block: (Bonus) -> Unit): Stuff {
contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) }
val stuff = grabStuffFromCargoBikeBasket()
val bonus = inspirationElixir()
block(bonus)
return stuff
}

In case you want to return multiple values of different types without using a class with destructuring declarations, you can also use a List of type Any for that purpose:

fun main() {
val (a, b, c) = getData()
println(a)
println(b)
println(c)
}


fun getData(): List<Any> {
val one = Int.MAX_VALUE
val two: List<Long> = listOf(1, 2, 3, 4)
val three: Pair<String, Char> = "a" to 'z'
return listOf(one, two, three)
}

This is the output:

2147483647
[1, 2, 3, 4]
(a, z)