Skip to content

Instantly share code, notes, and snippets.

@ajaypro
Last active June 6, 2019 08:46
Show Gist options
  • Save ajaypro/007d3628cd0552e03d8c631608b636f6 to your computer and use it in GitHub Desktop.
Save ajaypro/007d3628cd0552e03d8c631608b636f6 to your computer and use it in GitHub Desktop.
To have multiple return types using Pair and Triple in kotlin, Also read infixfunction, destructuring declaration of variables to combine and use with pair and triple
/**
* Usage of infix functio, Pair<String, Int> and multiple return types
*/
fun main() {
/** you can assign the variables of the Pair to another variable and use those
* variables like below
*/
//PAIR
val(var1, var2) = Pair("Bugatti", 56003)
println(var1)
println(var2)
// converting a Pair variable 'toString()'
val Car = Pair("Mclaren", 4567.45)
println(Car.toString())
// Using copy to change the value
val copyCar = Car.copy("Lamborgini")
println(copyCar)
// Converting a Pair variable to list and using it
val carDetails = Car.toList()
println(carDetails[0])
println(carDetails[1])
//Using the getCar()
val(model: String, price: Int) = getCar()
println(model)
println(price)
//TRIPLE
val(carmodel, carprice, cartype) = Triple("Benz", 56003.56f, listOf("sports", "luxury", "corporate"))
println(carmodel)
println(carprice)
println(cartype)
//Using components
val Cars = Triple("Bentley", 56003.56f, listOf("sports", "luxury", "domestic")
println("Using component")
println(Cars.component1())
println(Cars.component2())
println(Cars.component3())
}
//using destructure the variable declaration using 'to' infix function.
fun getCar(): Pair<String, Int>{
return "Ferrari" to 34562
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment