Programming in Kotlin - Part 5: Pair and Triple | Ray Wenderlich

Learn about two Kotlin classes, Pair and Triple, and get tips about when you should use each of them.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4736-programming-in-kotlin/lessons/5

Question, I see in the video you were creating a Pair(2.1, 3) then you printed them directly using .first and .second. As we see in the video the console prints out x=2.1 y=3 (considering that 2.1 is printed that way since is a float/double) But when you extract those values into separate variables such as x1 and y2 and you print those. it gets printed x1=2 y1=3, how did the first value of coordinates got transformed into an int, or why does it get printed as “2” and not “2.1”

If you look at line 5 you will see that coordinates is defined as Pair(2,3). We are not printing out coordinatesMixed, but the 2 and 3 of coordinates.

1 Like

Can you declare a variable like in Swift
var (a,b,c) : (Int,Double,String)
and then initialize
(a,b,c) = (1,2.0,“Hi”)
??

@kevindmoore Can you please help with this when you get a chance? Thank you - much appreciated! :]

What you are trying to do is destructure 3 variables from an object. If you used a Triple, you could do something like this. However, you can’t do what you have on the first line