How to return multiple values โ€‹โ€‹from a function in Kotlin, how do we do it quickly?

How to return 3 separate data values โ€‹โ€‹of the same type (Int) from a function in Kotlin?

I am trying to return the time of day, I need to return the Hour, Minute and Second as separate integers, but all in the same direction from the same function, is this possible?

In quick we do it as follows:

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

can we achieve this in Kotlin?

PS: I know that I can use Array or Hashmap for this, but I want to know if there is something in kotlin, as if it is fast.

+5
source share
5 answers

You cannot create arbitrary tuples in Kotlin, you can use data classes instead. One option uses the built-in Pair and Triple , which are generic and can contain two or three values, respectively. You can use them in combination with destruction declarations as follows:

 fun getPair() = Pair(1, "foo") val (num, str) = getPair() 

You can also destroy List or Array , 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, is 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() 
+10
source

According to Kotlin's documentation ( https://kotlinlang.org/docs/reference/multi-declarations.html#example-returning-two-values-from-a-function ) you can achieve this as follows:

 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() 
0
source

You can return multiple values โ€‹โ€‹as 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

see link for more details

0
source

There is no motorcade in Kotlin. Alternatively, you can use a data class with a declaration of destruction .

 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 do not want to create a data class for each specific case, you can create some general 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 use the destruction declaration to give it the correct property name.

 val time = getTime() println("${time.a}:${time.b}:${time.c}") val (hour, minute, second) = getTime() //destructuring declaration println("${hour}:${minute}:${second}") 
0
source

You seem to recognize the obvious answer to creating a specific class to handle time. Therefore, I think you are trying to avoid the small problems of creating a class or accessing each element of the array, etc. And you are looking for the shortest solution in terms of additional 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 to Array :

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

and

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

PS: you can use fewer variables than there are values โ€‹โ€‹when deconstructing, for example var (a, b) = getTime() , but you can not use more or you will get ArrayIndexOutOfBoundsException

0
source

Source: https://habr.com/ru/post/1273397/


All Articles