Reputation: 1050
Can you please help me in understanding the behaviour
//Creating a tuple
val myTuple = ("Sudipta","Deb","Switzerland",1234)
//> myTuple : (String, String, String, Int) = (Sudipta,Deb,Switzerland,1234)
myTuple._2 //> res0: String = Deb
myTuple._4 //> res1: Int = 1234
val (first, second, third, fourth) = myTuple //> first : String = Sudipta
//| second : String = Deb
//| third : String = Switzerland
//| fourth : Int = 1234
//val (first1, second1, _) = myTuple
Now last line is giving me the error:
constructor cannot be instantiated to expected type; found : (T1, T2, T3) required: (String, String, String, Int)
My Question is why it is behaving like this? In the Scala for Impatient Book this is what is written:
You can use a _ if you don’t need all components:
val (first, second, _) = t
Just for your reference, if you want to see the full code, it is in my GitHub Repository. Link: Scala Worksheet
Upvotes: 1
Views: 359
Reputation: 1847
You have to put one _
for every unused tuple member.
val (first1, second1, _, _) = myTuple
Upvotes: 3