你能帮我理解行为吗
//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
现在最后一行给了我错误:
constructor cannot be instantiated to expected type; found : (T1, T2, T3) required: (String, String, String, Int)
我的问题是为什么它的表现如此?在Scala for Impatient Book中写道:
You can use a _ if you don’t need all components:
val (first, second, _) = t
仅供您参考,如果您想查看完整代码,它就在我的GitHub存储库中。链接:Scala Worksheet
答案 0 :(得分:3)
您必须为每个未使用的元组成员添加一个_
。
val (first1, second1, _, _) = myTuple